Python | Extracting strings with Pandas .iloc []



Pandas provide a unique method for extracting strings from a data frame. Dataframe.iloc† is used when the data frame index label is something other than the numeric series 0, 1, 2, 3… .n or if the user does not know the label index. Rows can be retrieved using an imaginary index position that is not visible in the dataframe.

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

To download the CSV used in the code, click here.

Example # 1: Extract one line and compare with .loc []

This example is the same the ordinal string is retrieved by both the .iloc [] and .loc [] methods and compared. Since the index column is numeric by default, hence the index label will also be an integer.

# import pandas package

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "nba.csv" )

 
# extracting strings using the loc method

row1 = data.loc [ 3 ]

 
# extract rows using the iloc method

row2 = data.iloc [ 3 ]

  
# check if the values ​​are equal

row1 = = row2

Output:
As shown in the output image, the results returned by both methods are the same. 

Example # 2: Extracting multiple lines with index

This example retrieves multiple strings by first passing in a list and then passing in integers to extract the strings between that range. Then both values ​​are compared.

# pandas package import

import pandas as pd

 
# create a data frame from a CSV file

data = pd.read_csv ( " nba.csv " )

 
# retrieve rows using loc method

row1 = data.iloc [[[ 4 , 5 , 6 , 7 ]]

 
# extract strings using loc

row2 = data.iloc [ 4 : 8 ]

 
# compare values ​​

row1 = = row2

Output:
As shown in the output image, the results returned by both methods are the same. All values ​​are true except for the college column since they were NaN values.