Add a Column to Existing CSV File in Python
Last Updated :
28 Apr, 2025
Working with CSV files is a common task in data manipulation and analysis, and Python provides versatile tools to streamline this process. Here, we have an existing CSV file and our task is to add a new column to the existing CSV file in Python. In this article, we will see how we can add a column to a CSV file in Python.
Add a New Column to Existing CSV File in Python
Below, are examples of how to Add a New Column to an Existing CSV File in Python. Let's consider an example of an existing CSV file with some sample data. Suppose your existing CSV file, named
mon.csv
, looks like this:
mon.csv
mon.csvExample 1: Add New Column to Existing CSV Using Pandas
Pandas is a powerful data manipulation library in Python that makes working with tabular data seamless. Here's how you can use Pandas to add a new column to an existing CSV file: In this example, below Python code utilizes the Pandas library to add a new 'City' column with predefined values to an existing CSV file ('mon.csv'). It reads the CSV into a DataFrame, appends the new column, and writes the updated DataFrame back to the same CSV file.
Python3
import pandas as pd
# Step 1: Read the CSV file into a DataFrame
csv_file_path = 'mon.csv'
df = pd.read_csv(csv_file_path)
# Step 2: Define the values for the new "City" column
new_city_values = ['New York', 'Los Angeles', 'Chicago', 'San Francisco']
# Step 3: Add the new "City" column to the DataFrame
df['City'] = new_city_values
# Step 4: Write the DataFrame back to the CSV file
df.to_csv(csv_file_path, index=False)
Output:
updatedExample 2: Add New Column to Existing CSV Using CSV Module
The built-in csv
module in Python also allows us to work with CSV files. Here's how you can add a new column using the csv
module: In this example, below code reads the content of an existing CSV file named 'mon.csv' into a list of lists using the CSV module. It then adds a new column header 'City' to the first row and appends corresponding values to each subsequent row from the 'new_city_values' list
Python3
import csv
# Step 1: Read the existing CSV file and store its content
csv_file_path = 'mon.csv'
with open(csv_file_path, 'r') as file:
reader = csv.reader(file)
data = list(reader)
# Step 2: Define the values for the new "City" column
new_city_values = ['New York', 'Los Angeles', 'Chicago', 'San Francisco']
# Step 3: Add the new "City" column header to the first row of the data
data[0].append('City')
# Step 4: Add the new "City" column values to the remaining rows of the data
for i in range(1, len(data)):
data[i].append(new_city_values[i - 1])
# Step 5: Write the updated data back to the CSV file
with open(csv_file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
Output:
updated
Similar Reads
Delete a CSV Column in Python The comma-separated values ââ(CSV) file is a delimited text file that uses commas for individual values. Each line of the file is a data record in CSV. This format used for tabular data, rows, and columns, exactly like a spreadsheet. The CSV file stores data in rows and the values ââin each row are
3 min read
How to append a new row to an existing csv file? Appending a new row to a CSV file means adding data to the end of an existing dataset without modifying the previous entries. For example, if a CSV contains employee records, you might add a new row like [6, 'William', 5532, 1, 'UAE'] to update the file. First, let's take a look at the current conte
3 min read
How to delete a CSV file in Python? In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
2 min read
Exporting variable to CSV file in Python The CSV file or comma-separated values file is used to store and share data across platforms. The columns are separated by commas or other relevant delimiters, giving the data a tabular structure. Sometimes we come across ready-made CSV files while sometimes we need to create one according to the r
3 min read
Convert Excel to CSV in Python In this article, we will discuss how to convert an Excel (.xlsx) file to a CSV (.csv) file using Python. Excel files are commonly used to store data, and sometimes you may need to convert these files into CSV format for better compatibility or easier processing.Excel File Formats.xlsx: The newer Exc
3 min read