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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

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

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically