Append pandas DataFrame to Existing CSV File in Python (Example)

 

In this article you’ll learn how to append a new pandas DataFrame to an existing CSV file in Python programming.

Table of contents:

If you want to learn more about these topics, keep reading…

 

Example Data & Add-On Libraries

To be able to use the functions of the pandas library, we first have to load pandas:

import pandas as pd                                # Load pandas

We also need to create some data that we can use in the following examples.

data = pd.DataFrame({'x1':range(23, 27),           # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'd'],
                     'x3':range(15, 11, - 1)})
print(data)                                        # Print pandas DataFrame

 

table 1 DataFrame append pandas dataframe existing csv file python

 

As you can see based on Table 1, our example data is a pandas DataFrame consisting of four rows and three variables.

Let’s write these data to a CSV file:

data.to_csv('data.csv')                            # Export pandas DataFrame

After running the Python code above, you will find a CSV file containing our example data set in your working directory. We will use this CSV file as a basis for the following example.

Next, we have to construct a second pandas DataFrame that we will add below the data in our CSV file:

data_append = pd.DataFrame({'x1':range(123, 127),  # Create second pandas DataFrame
                            'x2':['x', 'x', 'y', 'y'],
                            'x3':range(115, 111, - 1)})
print(data_append)                                 # Print second pandas DataFrame

 

table 2 DataFrame append pandas dataframe existing csv file python

 

The output of the previous Python programming code is shown in Table 2: We have created another pandas DataFrame containing the same column names but different values as our first data set.

Let’s continue to the example!

 

Example: Concatenate pandas DataFrame to Existing CSV File

This example illustrates how to append a pandas DataFrame at the bottom of an already existing data set in a CSV file.

For this task, we can apply the to_csv function as shown below.

Within the to_csv function, we have to specify the name of the CSV file, we have to set the mode argument to be equal to ‘a’, and we have to specify that we want to ignore the header:

data_append.to_csv('data.csv',                     # Add new data vertically
                   mode = 'a',
                   header = False)

Once we have executed the Python syntax above, our example CSV file is updated so that the new data is concatenated vertically.

 

Video, Further Resources & Summary

Do you want to know more about the addition of a pandas DataFrame to an already existing CSV file? Then I recommend watching the following video on my YouTube channel. In the video, I demonstrate the examples of this article:

 

The YouTube video will be added soon.

 

In addition, you might read the related tutorials on this homepage. I have released numerous tutorials already.

 

You have learned in this tutorial how to concatenate and combine a pandas DataFrame to an already existing CSV file in the Python programming language. Let me know in the comments, in case you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top