Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

iloc() Function in Python

Python is one of the most popular programming languages used in data science and machine learning. One of the key features of Python is its powerful libraries that make it easier to work with large datasets. The Pandas library is one of the most commonly used libraries in Python, particularly for data manipulation and analysis. It provides powerful tools for data analysis, including the iloc function. In this article, we will explore the iloc function in Python and how it can be used to manipulate data.

What is iloc() Function in Python?

iloc stands for “integer location” and is a function in the Pandas library. It is used to select rows and columns from a Pandas DataFrame or a Series using integer-based indexing. The iloc function works by selecting rows and columns by their integer positions, rather than by their names. This means that the iloc function is particularly useful when working with large datasets, as it provides a faster and more efficient way to select data.

Syntax of iloc() Function in Python

The syntax of the iloc function in Python is as follows:

df.iloc[row_start:row_end, column_start:column_end]

In this syntax, “df” is the DataFrame that we want to select data from. The “row_start” and “row_end” arguments specify the starting and ending positions of the rows that we want to select. The “column_start” and “column_end” arguments specify the starting and ending positions of the columns that we want to select.

Parameters of iloc() Function in Python

The iloc function in Python takes one or two arguments to select specific rows and columns in a Pandas DataFrame. The arguments can take on different values depending on the specific use case. Here’s an overview of the different parameters of the iloc function:

  • row_start: This argument specifies the integer position of the starting row for the selection. If this parameter is not specified, it defaults to 0, which is the first row of the DataFrame.
  • row_end: This argument specifies the integer position of the ending row for the selection. If this parameter is not specified, it defaults to the last row of the DataFrame.
  • column_start: This argument specifies the integer position of the starting column for the selection. If this parameter is not specified, it defaults to 0, which is the first column of the DataFrame.
  • column_end: This argument specifies the integer position of the ending column for the selection. If this parameter is not specified, it defaults to the last column of the DataFrame.

Note that the row_end and column_end parameters are non-inclusive, meaning that the final row or column specified in the range is not included in the selection.

Return value of iloc() Function in Python

The iloc function in Python returns a view of the selected rows and columns from a Pandas DataFrame. This view can be used to access, modify, or delete the selected data.

The returned view is a Pandas DataFrame or Series, depending on the number of rows or columns selected. If a single row or column is selected, the returned object is a Pandas Series. If multiple rows or columns are selected, the returned object is a Pandas DataFrame.

Note that the returned view is a reference to the original DataFrame, rather than a copy. This means that any modifications made to the selected data will also affect the original DataFrame.

Here’s an example of using the iloc function and examining the returned view:

import pandas as pd

# Create a sample DataFrame
data = {'name': ['John', 'Mary', 'Alex', 'Emma'],
        'age': [28, 35, 42, 25],
        'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)

# Use iloc to select the first two rows and all columns
selected = df.iloc[0:2, :]

# Print the selected view
print(selected)

# Modify the selected view
selected['age'] = [30, 37]

# Print the original DataFrame to show the modification
print(df)

Output:

     name   age  gender
0   John    28       M
1   Mary    35       F

    name    age   gender
0   John     30       M
1   Mary     37       F
2   Alex      42       M
3   Emma   25       F

Explanation – In this example, the iloc function is used to select the first two rows and all columns of a DataFrame. The selected view is then printed, showing the selected data. Next, the age column of the selected view is modified. This modification also affects the original DataFrame, as demonstrated by printing the DataFrame again.

Examples of iloc() Function in Python

The iloc function is a powerful tool for selecting and manipulating data in Pandas DataFrames. Here are some examples of using the iloc function in Python, along with explanations of the code:

Example 1 – Selecting specific rows and columns
To select specific rows and columns from a DataFrame, you can use the iloc function with the row and column positions as arguments. For example:

import pandas as pd

# Create a sample DataFrame
data = {'name': ['John', 'Mary', 'Alex', 'Emma'],
        'age': [28, 35, 42, 25],
        'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)

# Select the second row and the age column
selected = df.iloc[1, 1]

print(selected)

Output:

35

Explanation: In this example, the iloc function is used to select the second row and the age column of a DataFrame. The selected value is then printed, showing the age of the second person in the DataFrame.

Example 2 – Selecting subsets of rows and columns
To select subsets of rows and columns from a DataFrame, you can use the iloc function with range values for the row and column positions. For example:

import pandas as pd

# Create a sample DataFrame
data = {'name': ['John', 'Mary', 'Alex', 'Emma'],
        'age': [28, 35, 42, 25],
        'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)

# Select the first two rows and all columns
selected = df.iloc[0:2, :]

print(selected)

Output:

    name   age  gender
0   John    28       M
1   Mary    35       F

Explanation: In this example, the iloc function is used to select the first two rows and all columns of a DataFrame. The selected data is then printed, showing the first two people in the DataFrame.

Example 3 – Slicing rows and columns
To slice rows and columns from a DataFrame, you can use the iloc function with a step value for the row and column positions. For example:

import pandas as pd

# Create a sample DataFrame
data = {'name': ['John', 'Mary', 'Alex', 'Emma'],
        'age': [28, 35, 42, 25],
        'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)

# Select every other row and column
selected = df.iloc[::2, ::2]

print(selected)

Output:

    name  gender
0   John      M
2   Alex       M

Explanation: In this example, the iloc function is used to select every other row and column of a DataFrame. The selected data is then printed, showing the name and gender of the first and third people in the DataFrame.

Summary

  • The iloc function is a tool in the Pandas library for selecting and manipulating data in DataFrames and Series.
  • It works by selecting rows and columns by their integer positions, rather than by their names.
  • The syntax of the iloc function includes arguments for row start, row end, column start, and column end.
  • The iloc function can be used to select specific rows and columns or to slice rows and columns from a DataFrame.

FAQs Related to iloc Function

Here are some frequently asked questions on iloc function in Python

Q1: What is the difference between iloc and loc in Pandas?
A: The iloc function is used to select rows and columns by their integer positions, while the loc function is used to select rows and columns by their labels or names.

Q2: What are some use cases for the iloc function?
A: The iloc function is particularly useful when working with large datasets, as it provides a faster and more efficient way to select data. Some use cases include selecting subsets of data, slicing data, and performing operations on specific rows or columns.

Q3: How can I use the iloc function to select specific rows and columns?
A: To select specific rows and columns using the iloc function, you can specify the row and column positions as arguments. For example, you can use the syntax df.iloc[row_position, column_position] to select a specific cell in a DataFrame.

Q4: Can I use iloc to filter rows based on a condition?
A: No, the iloc function is not used for filtering rows based on a condition. For filtering, you would typically use the Boolean indexing method in Pandas, which involves creating a Boolean expression to select the rows that meet a certain condition.

Q5: Is iloc the only way to select data in Pandas?
A: No, there are several methods for selecting and manipulating data in Pandas, including loc, ix, at, and iat. Each method has its own specific use cases and advantages.

Leave a Reply

Your email address will not be published. Required fields are marked *