Tsallaka zuwa abun ciki
Justice Tech School

Iterating Rows

iterrows gives rows and itertuples is faster. Prefer vectorised code.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
for t in df.itertuples():
    print(t.name, t.score)
for i, r in df.head(1).iterrows():
    print(i, r["name"])

Result

Ada 92
Kofi 78
Amina 85
Tunde 71
0 Ada

Exercise

Fast iteration.

df.()

Quiz Test

Is iterating usually best?

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