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.
Frequently Asked:
- Get Column Index from Column Name in Pandas DataFrame
- Replace NaN with 0 in Pandas DataFrame
- Select Rows where Two Columns are equal in Pandas
- Select Rows where Two Columns are not equal in Pandas
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.