In this tutorial, we will walk through how you can add a new column to an existing CSV (Comma Separated Value) file using Python. Python has libraries that make it easy to read, write and manipulate CSV files, predominantly pandas and csv.
We will employ the power of these libraries to accomplish our task. By the end of this tutorial, you will confidently be able to add a new column of data to an existing CSV file using Python.
Example file
Create a new file, called “filename.csv” and save it with the following data:
Name,Age,Location Alice,25,New York Bob,30,San Francisco Catherine,28,Los Angeles David,22,Chicago
Step 1: Installing the Necessary Libraries
The libraries we’ll need for this task are pandas and csv. If you haven’t already installed these libraries, install them using pip. Open your terminal and type the following command:
1 |
pip install pandas |
Do the same for the csv module:
1 |
pip install python-csv |
Step 2: Importing the Libraries
You need to import the necessary libraries into your script. At the top of your script, add:
1 2 |
import pandas as pd import csv |
We import pandas as pd to make referencing it in our code easier.
Step 3: Read the CSV File
You can read a CSV file in pandas using the read_csv() function. This function returns a dataframe.
1 |
df = pd.read_csv('filename.csv') |
Step 4: Add a New Column
To add a new column to the dataframe, follow this format:
1 |
df['new_column'] = new_column_values |
Step 5: Writing The Dataframe Back to The CSV
Now let’s write our dataframe with the new column back to the CSV file:
1 |
df.to_csv('filename.csv', index=False) |
Table with data from your file, after column adding:
Data from your CSV file
Full code:
1 2 3 4 5 6 |
import pandas as pd import csv df = pd.read_csv('filename.csv') df['new_column'] = new_column_values df.to_csv('filename.csv', index=False) |
Output
Conclusion
Adding a new column to an existing CSV file using Python is simple and straightforward with the use of pandas and csv libraries. Understanding these basics can help you manipulate CSV data in Python effectively.
Always remember to import the necessary libraries, read the CSV file using the pd.read_csv() function, add your new column, and write the dataframe back to the CSV. By following these steps, you can efficiently add new columns to any existing CSV file using Python.