How to add pandas data to an existing csv file?

You can specify a python write mode in the pandas to_csv function. For append it is ‘a’.

In your case:

df.to_csv('my_csv.csv', mode="a", header=False)

The default mode is ‘w’.

If the file initially might be missing, you can make sure the header is printed at the first write using this variation:

output_path="my_csv.csv"
df.to_csv(output_path, mode="a", header=not os.path.exists(output_path))

Leave a Comment