Panda's Write CSV - Append vs. Write

PythonCsvPandas

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

with open(filename, 'a') as f:
    df.to_csv(f, mode='a', header=f.tell()==0)

it will add header when writes to the file first time

Solution 3 - Python

In Pandas dataframe "to_csv" function, use header=False if csv file exists & append to existing file.

import os

hdr = False  if os.path.isfile('filename.csv') else True
df.to_csv('filename.csv', mode='a', header=hdr)

Solution 4 - Python

The above solutions are great, but I have a moral obligation to include the pathlib solution here:

from pathlib import Path

file_path = Path(filename)
if file_path.exists():
   df.to_csv(file_path, header=False, mode='a')
else:
   df.to_csv(file_path, header=True, mode='w')

Alternatively (depending on your inlining preferences):

file_exists = file_path.exists()
df.to_csv(file_path, header=not file_exists, mode='a' if file_exists else 'w')

Solution 5 - Python

Apart from file exist check, you can also check for non zero file size. Since it will make sense to add header if file exists but file size is zero i.e file without content. I find it helpful in some exceptional cases

import os.path
header_flag = False if (os.path.exists(fpath) and (os.path.getsize(fpath) > 0)) else True
df.to_csv(fpath, mode='a', index=False, header=header_flag)

Solution 6 - Python

In case if you have dict() and want to write and append into CSV file :

import pandas as pd

file_name = 'data.csv'

my_dict = {"column_1":"Apple","column_2":"Mango"}

with open(file_name, 'a') as f:
  
  df = pd.DataFrame(my_dict)
  df.to_csv(f, mode='a', header=f.tell()==0)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGPBView Question on Stackoverflow
Solution 1 - PythonPadraic CunninghamView Answer on Stackoverflow
Solution 2 - Pythonuser3657041View Answer on Stackoverflow
Solution 3 - PythonVK SinghView Answer on Stackoverflow
Solution 4 - PythonDV82XLView Answer on Stackoverflow
Solution 5 - PythonAlok NayakView Answer on Stackoverflow
Solution 6 - PythonVinay ChaudhariView Answer on Stackoverflow