| by Arround The Web | No comments

Pandas iloc()

Pandas is a popular Python library for data analysis that offers a variety of methods and functions for performing tasks, such as filtering, adding, and removing data. The “DataFrame.loc()” and “DataFrame.iloc()” methods can also be used to filter the data. We can utilize the “iloc()” method to filter out specific rows and columns using the index value of the Pandas DataFrame.

This guide will present you with a detailed tutorial on the “DataFrame.iloc[ ]” method using numerous examples via the below contents:

What is “DataFrame.iloc[ ]” in Python?

In Python, the “df.loc[ ]” method is used to retrieve the specific rows and columns from a DataFrame. This method performs integer-location-based indexing and selection by position. We can use this method to access elements in a DataFrame using their numerical position, starting from zero.

Syntax

dataframe.iloc[row, column]

 
Parameters

In the above syntax, the “row” and “column” parameters specify the index of the row and column.

Return Value

The return value of the “df.iloc[ ]” method of Pandas depends on the selection:

    • Single row/column: Pandas Series
    • Multiple rows/columns: Pandas DataFrame

Example 1: Selecting the DataFrame Single Row by Using the “iloc[ ]” Method

In this example code, we first created DataFrame and then used the “df.iloc[ ]” method to take the specified integer position of the DataFrame and retrieve the specified data of that row:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[2])

 
The following output shows the selection of the DataFrame single row:


Example 2: Selecting the DataFrame Multiple Row by Using the “iloc[ ]” Method

We can also select the DataFrame multiple rows by using the “iloc[ ]” method with double brackets. The indexes of the specified DataFrame rows have been passed to the “iloc[ ]” method and retrieve the data of multiple rows:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[[0,3]])

 
The below output shows that the DataFrame multiple rows have been selected successfully:


We can also use the slice notation for selecting the range of multiple rows. For example, in the below code, we specified the indexes “1” to “3” which means that all the rows from index “1” to “2” except the “3” have been selected and displayed:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[1:3])

 
The following output displays the specified rows to the output:


Example 3: Selecting the DataFrame Value of Specified Row and Column by Using the “iloc[ ]” Method

To select the specified DataFrame value, we can pass the row and column value to the “iloc[ ]” method. Here in the below code, the “iloc[ ]” method retrieves the value placed at row “3” and column “3” of DataFrame:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[3,3])

 
The below output shows the specified value of the DataFrame:


Example 4: Selecting the DataFrame Single Column by Using the “iloc[ ]” Method

To select the DataFrame single column the “iloc[ ]” method takes the colon as the first argument and the index of the DataFrame column as a second argument. Here, we use the index “3” to select the column label placed at index position “3” of the DataFrame:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[:, [3]])

 
The specified DataFrame column has been selected successfully:


Example 5: Selecting the DataFrame Multiple Column by Using the “iloc[ ]” Method

In this example code, the “iloc[ ]” method takes the multiple columns along with the colon as an argument. Here, we pass the index “0” and “3” to retrieve multiple column values of DataFrame:

import pandas
df1 = pandas.DataFrame({'Id':[1, 2, 3, 4],
                        'Name':['Joseph', 'Anna', 'Lily', 'Henry'],
                        'Age':[26, 25, 32, 41],
                        'Height':[4.5, 5.2, 3.8, 2.9]})
print(df1, '\n')
print(df1.iloc[:, [0,3]])

 
The following output shows the DataFrame multiple columns:

Conclusion

In Python, the “DataFrame.iloc[ ]” method is used to select or retrieve the single or multiple rows and column values by taking the index. We can also extract the value placed at specific row and column locations of the DataFrame. This guide provided a detailed guide on the “df.iloc[ ]” method of Pandas.

Share Button

Source: linuxhint.com

Leave a Reply