liveBook · Manning

Loop

As explained in Fast Python: High performance techniques for large datasets

A loop is a programming control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.

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

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Compares explicit indexing, iterrows, itertuples, apply, and vectorization, with timings for several approaches.
Examples
Uses taxi-trip records to compare calculations based on tip_amount and total_amount.
What you can do after
Apply pandas vectorization to avoid explicit row loops, or use itertuples when iteration remains necessary.
How it connects
Connects row iteration with pandas, DataFrame.apply, vectorization, and NumPy.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.