Skip to content

pandas/apply-axis-1

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

apply(axis=1) calls a Python function once for every row, which was hundreds of times slower than working on whole columns. Compute on the columns instead, as in df["a"] + df["b"].

total = df.apply(lambda row: row["load"] + row["wind"], axis=1)
total = df["load"] + df["wind"]

The rule reports df.apply(f, axis=1), and agg and transform with axis=1 and a lambda or a "_".join, which call a function once per row. On 100,000 rows the agg and transform forms were 327 to 2,735 times slower than whole columns. A named method, as in agg("sum", axis=1), runs on whole columns and is left alone.

xpd check --fix --unsafe-fixes rewrites simple arithmetic lambdas such as df.apply(lambda r: r["a"] + r["b"], axis=1) to df["a"] + df["b"]. Complex lambdas, agg, and transform forms remain manual.

apply(axis=1) calls a Python function once per row. Compute on whole columns instead, such as df["a"] + df["b"].

See all rules in the pandas pack.