Skip to content

pandas/filter-before-frame

Rule filter-before-frame
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

Filtering records in a Python comprehension before you build a frame tests every record in Python. Build the frame from all records, then select rows with a mask on the column.

frame = pd.DataFrame([row[1:] for row in rows if row[0] == "D"], columns=names)
frame = pd.DataFrame(rows)
frame = frame[frame[0] == "D"].drop(columns=0).set_axis(names, axis=1)

The rule reports pd.DataFrame([... for row in rows if ...]).

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

The comprehension tests every record in Python before the frame exists. A mask on the column compares all records at once. When the records come from a CSV file, pd.read_csv reads them without a Python loop.

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

This tests every record in Python before the frame is built. Build the frame from all records, and select the rows with a mask on the column.

Browse the other rules of the pandas pack.