A row loop can calculate tip amounts relative to total amounts by traversing every record. In a taxi-trip dataset, explicit indexing took minutes in the reported test; iterrows was only slightly better, while itertuples averaged 18 seconds. section 7.2.2
These implementations do not all calculate the same statistic: summing tips and totals before dividing differs from averaging each row’s tip-to-total ratio. The latter can be expressed with apply or vectorized column operations. Prefer vectorization when possible; if iteration is needed, itertuples is the faster of the tested loop approaches. section 7.2.2 section 7.6
def get_tip_mean_itertuples(df):
all_tips = 0
all_totals = 0
for my_tuple in df.itertuples():
all_tips += my_tuple.tip_amount
all_totals += my_tuple.total_amount
return all_tips / all_totals
An itertuples implementation that accumulates tip and total amounts across rows. section 7.2.2
def get_tip_mean_vector(df):
frac_tip = df["tip_amount"] / df["total_amount"]
return frac_tip.mean()
A vectorized column calculation that avoids an explicit row loop. section 7.2.2


























