Skip to content
Justice Tech School

loc and iloc

loc selects by labels and iloc by integer positions.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
print(df.loc[1, "name"])
print(df.loc[0:1, ["name", "score"]])
print(df.iloc[2, 0], df.iloc[:2, :2].shape)

Result

Kofi
   name  score
0   Ada     92
1  Kofi     78
Amina (2, 2)

Exercise

By position.

df.[0, 0]

Quiz Test

Is loc slicing inclusive?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.