I would like to use pd.write_csv to write "filename" (with headers) if "filename" doesn't exist, otherwise to append to "filename" if it exists. If I simply use command:
df.to_csv('filename.csv',mode = 'a',header ='column_names')
The write or append succeeds, but it seems like the header is written every time an append takes place.
How can I only add the header if the file doesn't exist, and append without header if the file does exist?
Appending dataframe means adding data rows to already existing files. To add a dataframe row-wise to an existing CSV file, we can write the dataframe to the CSV file in append mode by the parameter a using the pandas to_csv() function.
Append a dictionary as a new row to the existing CSV file Import DictWriter class from CSV module. Open your CSV file in append mode. Create a file object for this file. Pass the file object and a list of column names to DictWriter() You will get a object of DictWriter.
By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.
If you need to append row(s) to a CSV file, replace the write mode ( w ) with append mode ( a ) and skip writing the column names as a row ( writer.
Not sure there is a way in pandas but checking if the file exists would be a simple approach:
import os # if file does not exist write header if not os.path.isfile('filename.csv'): df.to_csv('filename.csv', header='column_names') else: # else it exists so append without writing the header df.to_csv('filename.csv', mode='a', header=False)
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