Python Practice › Pandas Programs › Program 47

Iterate over rows

EasyPandas Programs · 47 of 95

Write a pandas program to iterate over the rows.

Advertisement
Output
Yash    78
Rakesh  80
Suresh  60

Approach

  1. itertuples() is fast and gives named fields.

Solution

Python
import pandas as pddf = pd.DataFrame([{"name": "Yash", "percentage": 78}, {"name": "Rakesh", "percentage": 80}, {"name": "Suresh", "percentage": 60}])for row in df.itertuples(index=False):    print(f"{row.name:<7} {row.percentage}")
Output
Yash    78
Rakesh  80
Suresh  60
Tip

Prefer column operations over loops when you can; they are much faster on large data.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs