liveBook · Manning

Apply

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

In pandas, apply is a method that applies a callable across Series values or along a DataFrame axis and returns the results.

pandas apply processes rows individually with a callable, much like map. Setting axis=1 supplies each DataFrame row to the callable and returns a Series of results. section 7.2.2

Computing mean tip fraction

A row-wise lambda can divide tip_amount by total_amount, then mean() can aggregate the resulting fractions. This apply version took about 9.5 seconds, versus 18 seconds for itertuples; vectorization reduced the same computation to about 32 milliseconds. section 7.2.2

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

How deep it goes
Explains row-wise callable execution and benchmarks it, then defers to vectorization for substantially better performance.
Examples
Calculates taxi tip-to-total fractions with a row-wise lambda and compares timings against itertuples and vectorized Series arithmetic.
What it assumes
Assumes familiarity with pandas DataFrames, Series, callable expressions, and axis selection.
What you can do after
Evaluate apply-based row computations against iteration and vectorization using measured runtime rather than assumed performance.
How it connects
Connects apply with row iteration, map-style processing, vectorization, and dictionary or attribute-based value access.

Also explained in

Compare how 3 books explain apply →

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.