Python Pandas - How to select rows from a DataFrame by integer location


To select rows by integer location, use the iloc() function. Mention the index number of the row you want to select.

Create a DataFrame −

dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],index=['x', 'y', 'z'],columns=['a', 'b'])

Select rows with integer location using iloc() −

dataFrame.iloc[1]

Example

Following is the code −

import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],index=['x', 'y', 'z'],columns=['a', 'b'])

# DataFrame
print"DataFrame...\n",dataFrame

# select rows with loc
print"\nSelect rows by passing label..."
print(dataFrame.loc['z'])

# select rows with integer location using iloc
print"\nSelect rows by passing integer location..."
print(dataFrame.iloc[1])

Output

This will produce the following output −

DataFrame...
     a    b
x   10   15
y   20   25
z   30   35

Select rows by passing label...
a   30
b   35
Name: z, dtype: int64

Select rows by passing integer location...
a   20
b   25
Name: y, dtype: int64

Updated on: 16-Sep-2021

684 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements