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.
You can use the .iloc[] property to select a row by its integer index in a pandas DataFrame or Series. Note: .iloc[] selects by integer position, while .loc[] selects by label.
Select a row by its integer index in a pandas DataFrame or Series in Python
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:
Output
name Bob
age 30
Name: 1, dtype: objectSelect multiple rows by integer indices in a pandas DataFrame or Series in Python
rows = df.iloc[[0, 2]]
print(rows)This will output:
name age
0 John 25
2 Sara 35