Selecting a row of pandas series/dataframe by integer index
You can use the .iloc[]
property to select a row by its integer index in a pandas DataFrame or Series.
import pandas as pd
# Create a sample dataframe
data = {'name': ['John', 'Bob', 'Sara'], 'age': [25, 30, 35]}
df = pd.DataFrame(data)
# Select the row with index 1 (Bob)
row = df.iloc[1]
print(row)
This will output:
name Bob age 30 Name: 1, dtype: object
Watch a video course
Python - The Practical Guide
You can also select multiple rows by passing a list of indices to .iloc[]
, like so:
rows = df.iloc[[0, 2]]
print(rows)
This will output:
name age 0 John 25 2 Sara 35