6.2 C
London
Thursday, December 19, 2024
HomePandas in PythonInput/Output in PythonPandas: How to Append Data to Existing CSV File

Pandas: How to Append Data to Existing CSV File

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories