Skip to content

pandas/frame-growth-in-loop

Rule frame-growth-in-loop
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

Adding one row at a time with out.loc[len(out)] = row copies the frame on each pass. Collect the rows in a list and build the frame once.

out = pd.DataFrame(columns=output_columns)
for row in rows:
out.loc[len(out)] = row
out = pd.DataFrame(rows, columns=output_columns)

The rule reports out.loc[len(out)] = row inside a loop, which grows the frame by one row at a time.

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

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

Adding one row at a time copies the frame each time. Collect the rows in a list and build the frame once, as in pd.DataFrame(rows, columns=cols).

Browse the other rules of the pandas pack.