pandas/apply-axis-1
| Rule | apply-axis-1 |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
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"]What the rule reports
Section titled “What the rule reports”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.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
apply(axis=1) calls a Python function once per row. Compute on whole columns instead, such as df["a"] + df["b"].Browse the other rules of the pandas pack.