Python

Pandas Append to CSV

Pandas enables us to import the DataFrames, edit them, and then save them back after reading an external CSV or excel file. The append mode utilizes the “a” parameter which allows you to append the data to an existing CSV file. The “append to CSV” means that we add or merge the data to the existing CSV file by utilizing the “append()” function in “Pandas”. This tutorial outlines the “Pandas append to the CSV”. We will also illustrate the different codes here and append some data to the already created CSV file.

Syntax:
pandas.DataFrame_object.to_csv(file.csv, mode=’a’, index=False, header=False)

Parameters:

  1. We put the name of the CSV to which we want to append the data in the “file.csv” parameter.
  2. The “index” parameter is used to define the index. The “False” keyword means that the index does not appear after appending the data to the CSV file. We can also set it to “True” which means that the index value appears after appending.
  3. The “header” parameter is here to describe the header. The “False” keyword shows that the header is not included when we append the data. We can also utilize the “True” keyword which means that the header is inserted after appending.

Example 1: Append One Row

Let’s consider the CSV file named “Plan2.csv” having the following data:

Now, let’s create a Pandas DataFrame having one row and append to it.

import pandas

# Consider the DataFrame having one record
appending=pandas.DataFrame({'RSN ID':[1],
                        'RSN Name':['murthy'],
                        'Idea':['Construction developer'],
                        'Category':['All development']})

print("Appending Data: \n")
print(appending)

#  Append this row to the Plan.csv file
appending.to_csv("Plan2.csv",mode='a',index=False,header=False)

print("Appended one row successfully!")

Output:

Explanation:
The row is appended to the “Plan2.csv” file. Let’s open and see it.

Previously, there were 4 rows. Now, you can see that our record is added. Finally, there are 5 rows in this CSV file.

Example 2: Append Multiple Rows

Let’s consider a CSV file named “units1.csv”. It has 2 columns with 2 rows.

Now, let’s create a Pandas DataFrame having five rows and append to it.

import pandas

# Consider the DataFrame having 5 records
appending=pandas.DataFrame({
                        'Idea':['All','water supply','electricity','drilling','electricity'],
                        'demography':['ap','gujarat','patna','indore','norway']},index=[2,3,4,5,6])

print("Appending Data: \n")
print(appending)

#  Append these rows to the units1.csv file
appending.to_csv("units1.csv",mode='a',index=False,header=False)

print("Appended 5 rows successfully!")

Output:

Explanation:
The five rows are appended in the “units1.csv” file. Let’s open and see it.

Previously, there were 2 rows. Now, you can see that our records are added. Finally, there are 7 rows in this CSV file.

Example 3: Append the Rows with Index and Header

This scenario is not generally used. But anyway, we are going to see it in this example.

Let’s use the same CSV that we used in Example 2 and append the rows along with the header and index.

import pandas

# Consider the DataFrame having 5 records
appending=pandas.DataFrame({
                        'Idea':['All','water supply','electricity','drilling','electricity'],
                        'demography':['ap','gujarat','patna','indore','norway']},index=[2,3,4,5,6])

print("Appending Data: \n")
print(appending)

#  Append these rows to the units1.csv file
appending.to_csv("units1.csv",mode='a',index=True,header=True)

print("Appended 5 rows successfully!")

Output:

Explanation:
The five rows are appended in the “units1.csv” file. Let’s open and see it.

Previously, there were 2 rows. Now, you can see that our records are added. Finally, there are 7 rows in this CSV file. The index and headers also appended.

Conclusion

We provided a thorough explanation on how to append to the CSV in “Pandas” in this tutorial. We looked at three distinct examples to append the data to the already existing CSV file using the Pandas “to_csv()” function. Make sure that you need to set the index and header parameters to False while appending. After carefully reading this tutorial, I hope you learned the concept of the “to_csv()” approach and get the point on how to append the data to the CSV.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain