Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.
Extracting rows using Pandas .iloc[]
The Pandas library provides a unique method to retrieve rows from a DataFrame. Dataframe.iloc[] method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t know the index label. Rows can be extracted using an imaginary index position that isn’t visible in the Dataframe.
Pandas .iloc[] Syntax
Syntax: pandas.DataFrame.iloc[]
Parameters:
- Index Position: Index position of rows in integer or list of integer.
Return type: Data frame or Series depending on parameters
Dataset Used:
To download the CSV used in the code, click here.
Example 1: Extracting a single row and comparing with .loc[] In this example, the same index number row is extracted by both .iloc[] and.loc[] methods and compared. Since the index column by default is numeric, hence the index label will also be integers.
Python3
import pandas as pd
data = pd.read_csv(& quot
nba.csv & quot
)
row1 = data.loc[ 3 ]
row2 = data.iloc[ 3 ]
row1 = = row2
|
Output:
As shown in the output image, the results returned by both methods are the same.
Example 2: Extracting multiple rows with index In this example, multiple rows are extracted, first by passing a list and then by passing integers to extract rows between that range. After that, both values are compared.
Python3
import pandas as pd
data = pd.read_csv(& quot
nba.csv & quot
)
row1 = data.iloc[[ 4 , 5 , 6 , 7 ]]
row2 = data.iloc[ 4 : 8 ]
row1 = = row2
|
Output:
As shown in the output image, the results returned by both methods are the same. All values are True except values in the college column since those were NaN values.