How to Append DataFrame to Existing CSV File via Pandas
When you already have a DataFrame, you can easily append it to a csv file.
Just enable mode=’a’ in the settings of to_csv
Please use following syntax in pandas to append data to an existing CSV file:
df.to_csv('existing.csv', mode='a', index=False, header=False)
Here is a specific explanation of the parameters of the function to_csv():
- ‘existing.csv’: The name of the existing CSV file. (It will create one automatically if not existing)
- mode=’a’: Use the ‘append’ mode. the default mode is ‘w'(write only)
- index=False: Do not include an index column when appending the new data.. (Would suggest to use index=False)
- header=False: Do not include a header when appending the new data. (If you select header=True, you will include a header again)
The following step-by-step example shows how to use this function in practice.
Step 1: Create a new CSV File
import pandas as pd
#create DataFrame
df = pd.DataFrame({'team': ['D', 'D', 'E', 'E'],
'points': [2, 4, 5, 7],
'rebounds': [15, 13, 9, 12]})
#view DataFrame
df
team points rebounds
0 D 2 15
1 D 4 13
2 E 5 9
3 E 7 12
##create a csv
df.to_csv('/temp.csv',header=True)
Step 2: Create New Data to Append
Let’s create a new pandas DataFrame to append to the existing CSV file:
#create DataFrame
df = pd.DataFrame({'team': ['D', 'D', 'F', 'F'],
'points': [6, 4, 4, 8],
'rebounds': [15, 18, 44, 12]})
#view DataFrame
df
team points rebounds
0 D 6 15
1 D 4 18
2 F 4 44
3 F 8 12
Step 3: Append New Data to Existing CSV
Use below code to append this new data to the existing CSV file:
df.to_csv('/temp.csv', mode='a', index=False, header=False)
Step 4: View Updated CSV
When we open the existing CSV file, we can see that the new data has been appended
Notes on Appending Data
Index would impact your result.
When appending data to an existing CSV file, you need to check whether the existing CSV has an index column or not.
If the existing CSV file does not have an index file, you need to specify index=False in the to_csv() function when appending the new data to prevent pandas from adding an index column. (It may impact your result)
See more technical help in https://growingnerds.com