Skip to content

pandas/zip-over-columns

Rule zip-over-columns
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

A loop over zipped columns runs Python once for every row. Compute on whole columns instead.

out = [load * weight for load, weight in zip(df["load"], df["weight"])]
out = (df["load"] * df["weight"]).tolist()

The rule reports a loop or comprehension over zip(df["a"], df["w"]), which works on one pair of values at a time. It reports only when every argument of zip is a column taken by a text label, or a .values, .to_numpy() or .tolist(). It leaves dict(zip(...)) alone, because it isn't a loop. The rule doesn't run on test files.

xpd has no automatic fix, so make the change by hand.

Keep the result as a Series when the next step takes one, and call tolist only where the next step needs a list.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

A loop over zipped columns runs Python once per row. Compute on whole columns instead, such as df["a"] * df["w"].

Browse the other rules of the pandas pack.