Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Panda's Write CSV - Append vs. Write

Tags:

python

pandas

csv

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?

like image 970
GPB Avatar asked Jun 22 '15 23:06

GPB


People also ask

Does Pandas to CSV append?

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.

What is append in CSV?

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.

How write CSV file in Pandas?

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.

Can we append in CSV file?

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.


1 Answers

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) 
like image 114
Padraic Cunningham Avatar answered Oct 06 '22 00:10

Padraic Cunningham