Skip to content

pandas/to-csv-append-header

Rule to-csv-append-header
Pack pandas
Severity error
Fix none, edit by hand
Library versions every version

to_csv(mode="a") writes the header again on every call, so header rows end up among the data. Pass header=False, or write the header only on the first call.

for part in parts:
part.to_csv(path, mode="a", index=False)
part.to_csv(path, mode="a", index=False, header=not os.path.exists(path))

The rule reports df.to_csv(path, mode="a") without header=, which writes the header again on every call.

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

The repeated header rows are read back as data, so every number in the file comes back as text.

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

to_csv with mode="a" writes the header again on every call, so the file holds header rows among the data. Pass header=False, or header=not os.path.exists(path) for the first write.

Browse the other rules of the pandas pack.