Informatics Practices · Class 12 · Python Pandas · DataFrame
PandasDataFrame⏱️ 6 min read
Iterating over a DataFrameOptional Reading
Sometimes you need to visit every row (or column) one at a time. Rather than write a statement for each, you iterate — a for loop that hands you one piece per turn.
1Row by row with iterrows()
df.iterrows() yields a (index, Series) pair each turn: the row's label and all its values as a Series. Perfect for "do something with each student".
iterrows.py
Tip
Inside the loop, grab one value with
row['IP'] — because row is just a Series. Try adding print(row_index, row['Name'], row['IP']).2Column by column with items()
df.items() goes the other way — one (column name, Series) pair per turn, walking across the columns.
items.py
Note
Older material (and the RM) calls the column version
iteritems(). Modern Pandas renamed it to items() — they do the same thing.Key Takeaway
iterrows() → one row (index, Series) per turn. items() → one column (name, Series) per turn. Either way, each piece is a Series you already know how to use. Quick Check
Each turn of `for i, r in df.iterrows()`, what is r?
Quick Check
Which loop visits the DataFrame column by column?