How to Append Pandas DataFrame to Existing CSV File?
Last Updated :
06 Dec, 2023
In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python.
Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv()
function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mode.
Pandas DataFrame to_csv() Syntax
Syntax :
df.to_csv('existing.csv', mode='a', index=False, header=False)
Parameters:
- existing.csv: Name of the existing CSV file.
- mode: By default mode is 'w' which will overwrite the file. Use 'a' to append data into the file.
- index: False means do not include an index column when appending the new data. True means include an index column when appending the new data.
- header: False means do not include a header when appending the new data. True means include a header when appending the new data
What is DataFrame to_csv() Function ?
The Pandas DataFrame `to_csv()` function is a method that allows you to export a DataFrame to a CSV (Comma-Separated Values) file. This function enables you to save the contents of a DataFrame into a CSV format, which is a widely used file format for tabular data. The method provides various parameters to customize the export, such as specifying the file path, choosing the delimiter, and handling missing values.
Append Pandas Dataframe to Existing CSV File
Below are the steps to Add Pandas Dataframe to an Existing CSV File.
Step 1: View Existing CSV File
First, find the CSV file in which we want to append the dataframe. We have an existing CSV file with player name and runs, wickets, and catch done by the player. And we want to append some more player data to this CSV file. This is how the existing CSV file looks:

Step 2: Create New DataFrame to Append
Now let's say we want to add more players to this CSV file. First create a dataframe of that player with their corresponding run, wicket, and catch. And make their pandas dataframe. We will append this to the existing CSV file.
Step 3: Append DataFrame to an Existing CSV File in Pandas
Let's append the dataframe to the existing CSV file. Below is the python code.
Python3
# Append Pandas DataFrame to Existing CSV File
# importing pandas module
import pandas as pd
# data of Player and their performance
data = {
'Name': ['Hardik', 'Pollard', 'Bravo'],
'Run': [50, 63, 15],
'Wicket': [0, 2, 3],
'Catch': [4, 2, 1]
}
# Make data frame of above data
df = pd.DataFrame(data)
# append data frame to CSV file
df.to_csv('GFG.csv', mode='a', index=False, header=False)
# print message
print("Data appended successfully.")
Output :

Similar Reads
How to export Pandas DataFrame to a CSV file? Let us see how to export a Pandas DataFrame to a CSV file. We will be using the to_csv() function to save a DataFrame as a CSV file. DataFrame.to_csv() Syntax : to_csv(parameters) Parameters : path_or_buf : File path or object, if None is provided the result is returned as a string. sep : String of
3 min read
Export Pandas dataframe to a CSV file When working on a Data Science project one of the key tasks is data management which includes data collection, cleaning and storage. Once our data is cleaned and processed itâs essential to save it in a structured format for further analysis or sharing.A CSV (Comma-Separated Values) file is a widely
2 min read
How to add one row in existing Pandas DataFrame? Adding rows to a Pandas DataFrame is a common task in data manipulation and can be achieved using methods like loc[], and concat(). Method 1. Using loc[] - By Specifying its Index and ValuesThe loc[] method is ideal for directly modifying an existing DataFrame, making it more memory-efficient compar
4 min read
Adding New Column to Existing DataFrame in Pandas Adding a new column to a DataFrame in Pandas is a simple and common operation when working with data in Python. You can quickly create new columns by directly assigning values to them. Let's discuss how to add new columns to the existing DataFrame in Pandas. There can be multiple methods, based on d
6 min read
How to append a new row to an existing csv file? Appending a new row to a CSV file means adding data to the end of an existing dataset without modifying the previous entries. For example, if a CSV contains employee records, you might add a new row like [6, 'William', 5532, 1, 'UAE'] to update the file. First, let's take a look at the current conte
3 min read