Home » Pandas: How to Append Data to Existing CSV File

Pandas: How to Append Data to Existing CSV File

by Erma Khan

You can use the following syntax in pandas to append data to an existing CSV file:

df.to_csv('existing.csv', mode='a', index=False, header=False)

Here’s how to interpret the arguments in the to_csv() function:

  • ‘existing.csv’: The name of the existing CSV file.
  • mode=’a’: Use the ‘append’ mode as opposed to ‘w’ – the default ‘write’ mode.
  • index=False: Do not include an index column when appending the new data.
  • header=False: Do not include a header when appending the new data.

The following step-by-step example shows how to use this function in practice.

Step 1: View Existing CSV File

Suppose we have the following existing CSV file:

Step 2: Create New Data to Append

Let’s create a new pandas DataFrame to append to the existing CSV file:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['D', 'D', 'E', 'E'],
                   'points': [6, 4, 4, 7],
                   'rebounds': [15, 18, 9, 12]})

#view DataFrame
df

        team	points	rebounds
0	D	6	15
1	D	4	18
2	E	4	9
3	E	7	12

Step 3: Append New Data to Existing CSV

The following code shows how to append this new data to the existing CSV file:

df.to_csv('existing.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

When appending data to an existing CSV file, be sure 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.

Additional Resources

How to Export Pandas DataFrame to CSV
How to Export Pandas DataFrame to Excel
How to Export Pandas DataFrames to Multiple Excel Sheets

Related Posts