I wonder how to add new DataFrame
data onto the end of an existing csv file? The to_csv
doesn't mention such functionality.
Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.
to_csv() function write the given series object to a comma-separated values (csv) file/format. Parameter : path_or_buf : File path or object, if None is provided the result is returned as a string.
Here's how to interpret the arguments in the to_csv() function: 'existing. csv': The name of the existing CSV file. mode='a': Use the 'append' mode as opposed to 'w' – the default 'write' mode. index=False: Do not include an index column when appending the new data.
You can append using to_csv
by passing a file which is open in append mode:
with open(file_name, 'a') as f: df.to_csv(f, header=False)
Use header=None
, so as not to append the column names.
In fact, pandas has a wrapper to do this in to_csv
using the mode
argument (see Joe's answer):
df.to_csv(f, mode='a', header=False)
You can also pass the file mode as an argument to the to_csv method
df.to_csv(file_name, header=False, mode = 'a')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With