In this article we will discuss how to add a column to an existing CSV file using csv.reader and csv.DictWriter classes. A part from appending the columns we will also discuss how to insert columns in between other columns of the existing CSV file.

There are no direct functions in a python to add a column in a csv file. Although in python we have a csv module that provides different classes for reading and writing csv files. All the reading and writing operations provided by these classes are row specific. But we can build our logic on top of these functions to add or append columns in a csv file. Let’s see how to do that,

Add a column with same values to an existing CSV file

Suppose we have a CSV file i.e. input.csv and its contents are,

Now we want to add a column in this csv file and all the entries in this column should contain the same value i.e. Some Text.

Let’s see how to do that,

csv.reader class in python’s csv module provides a mechanism to read each row in the csv file as a list. Whereas, csv.writer class in python’s csv module provides a mechanism to write a list as a row in the csv file.

Steps will be to append a column in csv file are,

  • Open ‘input.csv’ file in read mode and create csv.reader object for this csv file
  • Open ‘output.csv’ file in write mode and create csv.writer object for this csv file
  • Using reader object, read the ‘input.csv’ file line by line
    • For each row (read as list ), append default text in the list.
    • Write this updated list / row  in the ‘output.csv’ using csv.writer object for this file.
  • Close both input.csv and output.csv file.

Code is as follows,

It appended the column in the contents of input.csv and saved the changes as output_1.csv file. Contents of output_1.csv file are,

Adding column in CSV based on custom logic

In the above example we added a column of same values in the csv file. But there can be other scenarios, like,

  • How to add column in csv file based on some other logic,  where each value in the column is  different ?
  • How to add a list as a column in csv file?
  • How to add column in csv file with header?
  • How to insert columns in between other columns of the csv file.

We can change the little code in the above example, for each of our scenarios but that will be too much duplication of code.

So, lets create a generic function that will perform the most common part for adding a column in csv file i.e.

This function has 3 arguments,

  •     input_file: file path / name of the input csv file, it will read the contents of this csv file
  •     output_file: file path / name of the output csv file, it will write modified contents in this csv file
  •     transform_row: A callback function, that receives a list and modifies that list

This function iterates over each row of the input_file and read the contents of each row as a list. Then it passes that list into a transform_row() function for modification. In side this callback we can modify the list like add an entry in it. Then it saves the modified row / list in the output_file.

In the transform_row argument of this function, we will mainly pass a lambda function in our examples.

So, let’s use this function to solve our problems,

Add a column with same values to an existing csv file using generic function & a lambda

We did the same in our first example, but let’s do it again using our generic function and a lambda i.e.

It appended the column in the contents of input.csv and saved the changes as output_2.csv file. Contents of output_2.csv file are,

Add a column to an existing csv file, based on values from other columns

Let’s append a column in input.csv file by merging the value of first and second columns i.e.

In the lambda function we received each row as list and the line number. It then added a value in the list and the value is a merger of first and second value of list.

It appended the column in the contents of input.csv by merging value of first and second column and then saved the changes as output_3.csv file.

Contents of output_3.csv file are,

Add a list as a column to an existing csv file

Suppose we have a list of string i.e.

Let’s add this list of strings as last column in input.csv file and save its contents as output_4.csv,

In the lambda function we received each row as list and the line number. It then added a value in the list and the value is an entry from our list_of_str at index  line_num – 1.

Thus all the entries in the list_of_str are added as a column in the csv.

Contents of output_4.csv file are,

Insert a column as second column with same values into an existing csv

In all the above examples we added a column in the end of csv file i.e. as last column. What if we want to insert a new column in between other columns of the csv file ? Let’s see how to do that,

Insert a column in input.csv file as the second column and save its contents to output_5.csv file,

Contents of output_5.csv file are,

In the lambda function we received each row as list and the line number. It then inserted the value at the end position the list.

Add a column with same values to an existing csv file with header

In all the above examples we added column in a csv file that didn’t had any header. If our csv file has a header too, 

then let’s see how to add a new column with header and values i.e.

In the lambda function it receives the row as list and its line number as arguments. Then checks if line_num is 1, then it means this list contains column names and it adds the name of column in it. Whereas, if line_num is not 1 then it means its a normal row of csv file, if adds the entry of new column in it.

So, it added a new column with values ‘Same_Text’ and header ‘Address’. Then saves that content as the output_6.csv,

Contents of output_6.csv file are,

Use DictReader DictWriter to add a column in existing csv file

Python’s csv module provides two other class for reading and writing contents in the csv file i.e. DictReader & DictWriter. It performs all the operations using dictionaries instead of lists.

We have created a separate function to add column in csv file, its similar to previous one but it uses DictReader  and DictWriter class instead,

In this function we need to pass an additional callback tansform_column_names, it receives list of column names and we can modify that based on our intent.

Let’s see how to use this function to solve our problems,

Use DictReader DictWriter to add a column with same values to an existing csv

Contents of output_7.csv file are,

Use DictReader DictWriter to insert a column as second column in a csv’

Contents of output_7.csv file are,

The complete example is as follows,

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.