Add a header to a CSV file in Python

In this article, we will learn about different ways to add a header to a csv file in Python.

Table Of Contents

Introduction

Suppose we have a csv file, which contains only data rows. For example,

Ritika,27,Delhi,India
Mark,28,Sydney,Australia
Suse,29,Las Vegas,USA
Shaun,30,London,UK

Now we want to add a header row in this csv file. Modified csv file should be like this,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,Australia
Suse,29,Las Vegas,USA
Shaun,30,London,UK

Basically we want to insert a row at the top of csv file, and shift all the existing row after it. There are multiple ways to do this. Let’s discuss them one by one,

Advertisements

Add header to a CSV file using Pandas

Pandas module in Python, provides a function read_csv(), to read the contents of csv and intialize a DataFrame object with it. By default it considers the first line of csv as header. But if we pass the header parameter as None in the read_csv() function, it will read all the rows of csv as data row and will initialize a DataFrame without a header. Then we write the DataFrame to same csv again, but this time with a header. For that, we will use the to_csv() function of DataFrame, and we will pass a list of strings has heder of csv file. Let’s see an example,

import pandas as pd

# Read csv file to a DataFrame
df = pd.read_csv('employees.csv', header = None)

# Write DataFrame to csv file, but with header
df.to_csv(
    "employees.csv",
    header=["Name", "Age", "City", "Country"],
    index=False)

It will modify the contents of file employees.csv. The csv file will now have a header. Contents of the employees.csv file will be like,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,Australia
Suse,29,Las Vegas,USA
Shaun,30,London,UK

It added the header to the csv file.

Basically, we read the csv file to a DataFrame, and while saving it back to a csv file, we added the header in it.

Add header to a CSV file using csv module

The process will be broken into two steps,

  • Read data from csv file into a list of lists.
    • Open csv file with open() function in read mode, and get a file object. Pass this file object to the reader() function of csv module, to get a csv reader object. Then add all lines of csv file to a list of lists using it. Where, each sublist represents a row of csv file. Then, close the csv file.
  • Rewrite the csv file again by first writing the header, and then adding list of lists as rows in the csv file.
    • Open csv file again with open() function in write mode, and get a file object. It will clean the data in file, and set the file pointer to the starting of file. Then, pass this file object to the writer() function of csv module to get a csv writer object. Then call its writerow() function with a list of string to add it as header in csv file. After that, call the writerows() of writer object with list of lists, to add them as rows in the csv file.

Let’s see a complete example,

import csv

header = ["Name", "Age", "City", "Country"]

listOfRows = []

# Read all lines of csv file as list of lists
with open("employees.csv", 'r') as fileObj:
    readerObj = csv.reader(fileObj)
    listOfRows = list(readerObj)

# Open csv file
with open("employees.csv", 'w') as fileObj:
    writerObj = csv.writer(fileObj)
    # Add header to csv file
    writerObj.writerow(header)
    # Add list of lists as rows to the csv file
    writerObj.writerows(listOfRows)

It will modify the contents of file employees.csv. The csv file will now have a header. Contents of the employees.csv file will be like,

Name,Age,City,Country
Ritika,27,Delhi,India
Mark,28,Sydney,Australia
Suse,29,Las Vegas,USA
Shaun,30,London,UK

It added the header to the csv file.

Basically, it read the csv file contents, and stored them in a list of lists. Then while saving it back to a csv file, we added the header in it first.

Summary

We learned about different ways to add a header to a CSV file in Python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top