Change language

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.

Python | Extracting strings with Pandas .iloc [] File handling: Questions

Python | Extracting strings with Pandas .iloc [] Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method