iterrows traverses a pandas DataFrame with a for loop, yielding each row’s current position and row data. It offers a familiar baseline, but remains slow for substantial computations. section 7.2.2
Use iterrows cautiously when working with row-oriented logic:
- Expect only a slight improvement over explicit indexing; both approaches can take minutes. section 7.2.2
- Prefer itertuples when a loop is unavoidable; the same calculation falls to about 18 seconds in the comparison. section 7.2.2
- Move toward apply or vectorization for row calculations; apply reaches 9.5 seconds, while vectorization avoids row-wise iteration. section 7.2.2
def get_tip_mean_iterrows(df):
all_tips = 0
all_totals = 0
for i, row in df.iterrows():
all_tips += row["tip_amount"]
all_totals += row["total_amount"]
return all_tips / all_totals
A minimal iterrows implementation computes aggregate tip and total amounts while exposing the iterator’s index-and-row interface. section 7.2.2
