Add New Column to Existing CSV File in Python (Example)

 

In this tutorial you’ll learn how to add a new column to a CSV file in the Python programming language.

The content of the post looks as follows:

Let’s get started!

 

Example Data & Add-On Libraries

We first have to load the pandas library, in order to apply the corresponding functions:

import pandas as pd                                          # Import pandas library

Furthermore, have a look at the exemplifying pandas DataFrame below:

data = pd.DataFrame({'x1':range(10, 17),                     # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'd', 'e', 'f', 'g'],
                     'x3':[3, 9, 7, 7, 3, 2, 5],
                     'x4':range(15, 8, - 1)})
print(data)                                                  # Print pandas DataFrame

 

table 1 DataFrame add new column existing csv file python

 

Have a look at the previous table. It shows that our example data contains seven rows and four columns.

Let’s export this data set to a CSV file that we can use for the following example:

data.to_csv('data.csv', index = False)                       # Export pandas DataFrame

Furthermore, consider the following list object. In the following example, we will add this list as a new column to our pandas DataFrame:

new_col = ['foo', 'bar', 'foo', 'foo', 'bar', 'bar', 'bar']  # Create list object
print(new_col)                                               # Print list object
# ['foo', 'bar', 'foo', 'foo', 'bar', 'bar', 'bar']

Now, we are set up and can move on to the example code…

 

Example: Appending a New Column to a pandas DataFrame in a CSV File

In this example, I’ll illustrate how to append a new variable to a pandas DataFrame in a CSV file in Python.

For this task, we first have to import the CSV file using the read_csv function:

data_new = pd.read_csv('data.csv')                           # Read CSV file

In the next step, we can add our list as a new column to our pandas DataFrame:

data_new['new_col'] = new_col                                # Append list as new variable

Finally, we can export the updated pandas DataFrame to a new CSV file as shown below:

data_new.to_csv('data_new.csv')                              # Export new pandas DataFrame

This CSV file will contain our input data set as well as the example list as a new variable.

Note that we could specify the same CSV file name within the to_csv function to overwrite our input file. This has to be done with care though, because it will be difficult to restore the old file in case something goes wrong.

 

Video, Further Resources & Summary

Do you need further explanations on the Python programming code of this article? Then I can recommend having a look at the following video on my YouTube channel. I illustrate the content of this article in the video.

 

The YouTube video will be added soon.

 

Furthermore, you might want to read the related articles on this website. Some articles can be found below:

 

In summary: At this point of the tutorial you should know how to combine and append a new variable to a CSV file in the Python programming language. In case you have additional comments or questions, don’t hesitate to let me know in the comments.

 

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