Select with iloc
iloc selects by integer position, like Python slicing, so the end is excluded.
Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
print(df.iloc[0, 0])
print(df.iloc[1:3, [0, 3]])
print(df.iloc[-1].tolist())Result
Ada name score 1 Bob 92.0 2 Cy 79.5 ['Di', np.int64(28), 'Nairobi', np.float64(85.0)]
Exercise
First row.
df.iloc[]
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.