Skip to content

pandas/column-loop-to-long

Pack Severity Fix Versions
pandas info none, edit by hand All versions

A loop that builds one frame per column and stacks them does what melt does in one call. Use df.melt with id_vars and value_vars, which gave the same rows faster.

parts = [df[["id", year]].rename(columns={year: "sales"}).assign(year=year) for year in years]
long = pd.concat(parts, ignore_index=True)
long = df.melt(id_vars="id", value_vars=years, var_name="year", value_name="sales")

The rule reports a loop over column names that selects one column at a time, renames it to a shared name and stacks the parts with pd.concat. The loop can be a list comprehension, passed to pd.concat directly or through a name that the same function passes to it, or a for statement that appends. For 100,000 rows and 20 columns, melt gave the same 2,000,000 rows in the same order and ran 1.4 times faster.

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

This builds one frame per column and stacks them. melt does that in one call, as in df.melt(id_vars=["id"], value_vars=columns, var_name="year", value_name="sales"). For 20 columns it gave the same rows in the same order and ran 1.4 times faster.

See all rules in the pandas pack.