How to add Pandas DataFrame to an existing csv file?

In this article, we will discuss the method to add pandas data to an existing CSV file.

Table of Content

Introduction to DataFrame.to_csv()

We are going to use the DataFrame.to_csv() function for the same, so let’s understand the attributes of the function.

df.to_csv(filename, mode='a', index=False, header=False)
  • filename: new or existing file name
  • mode: By default, the mode is ‘w’ which overwrites the file. We will use ‘a’ to append data to the file
  • index: False will ignore the data index while appending the data
  • header: False will ignore the column headers while appending the data

Preparing csv file

To quickly get started, we have already created a CSV file (“employees.csv”) containing the following information.

Name,Age,Experience,RelevantExperience
Shubham,25,5,4
Riti,30,7,7
Shanky,23,2,2
Shreya,24,2,0
Aadi,33,11,5
Sim,28,4,4

Create a new DataFrame to append

To start with, let’s create new data to append to the above file. We are going to use pandas.DataFrame() function with some random data.

import pandas as pd

# random data
df = pd.DataFrame({
        "Name": ['Abhishek', 'Tinky'],
        "Age": [35, 40],
        "Experience": [12, 15],
        "RelevantExperience": [8, 11]
    },
    index = ['G', 'H'])

print(df)

Output

       Name  Age  Experience  RelevantExperience
G  Abhishek   35          12                   8
H     Tinky   40          15                  11

As observed, we have the new DataFrame ready to be appended to the existing file.

Append DataFrame into existing file

We are going to use the DataFrame.to_csv() function with the “a” mode to append the data into the “employees.csv” file. Let’s understand with the code below.

# append the data into existing file
df.to_csv("employees.csv", mode='a', index=False, header=False)

Now, let’s look at the “employees.csv” file again.

Output

Name,Age,Experience,RelevantExperience
Shubham,25,5,4
Riti,30,7,7
Shanky,23,2,2
Shreya,24,2,0
Aadi,33,11,5
Sim,28,4,4
Abhishek,35,12,8
Tinky,40,15,11

As observed, we have two additional rows now coming from the data we created above. Therefore, our data is successfully appended to the existing file.

Summary

In this article, we have discussed how to add pandas data to an existing CSV file. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top