Python Practice › Pandas Programs › Program 47
Iterate over rows
Write a pandas program to iterate over the rows.
Output
Yash 78 Rakesh 80 Suresh 60
Approach
itertuples()is fast and gives named fields.
Solution
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.
Learn the concept
New to this topic? Read the lesson, then come back.