In this tutorial, we’ll try to understand how to add a header in a csv file using Python with the help of some examples.
In simple terms, a csv file contains tabular data with comma-separated values in each row. A row header, here, represents an additional row at the top indicating the column names.
You can use the following methods to add a header to a csv file using Python –
- Using the pandas dataframe
- Using the
csvmodule in Python.
Let’s now look at the above methods in detail. We’ll work with a csv file without a header to demonstrate the examples. This is how the csv file looks.
Using the pandas dataframe
Steps to add a header to a csv file using the
pandas.DataFrame.to_csv() method are.
- Import the pandas module.
- Read the csv file as a pandas DataFrame object (pass
header=Noneas an additional parameter).
- Save the dataframe as a csv file (along with the specified header) by using the pandas dataframe
Now, let us try to understand the above method, with some worked out examples.
import pandas as pd # read the csv file as a dataframe df = pd.read_csv("students.csv", header=None) # display the dataframe print(df)
0 1 2 0 14 Tony Male 1 15 Emma Female 2 14 Rishi Male 3 16 Zara Female
We are reading the csv file with the pandas
read_csv() function and pass
header=None because we know that the csv file doesn’t have any headers. If we do not pass
header=None, the function will infer the header by itself and since this file doesn’t have a header, it will use the first row as the header (which we do not want).
You can see that the dataframe doesn’t have any headers.
Let’s save this dataframe as a csv file with a header. For this, use the
pandas.DataFrame.to_csv() function and pass the header values as a list to the
# header values for the data header_ls = ['Age', 'Name', 'Gender'] # save dataframe as a csv file with the given headers df.to_csv("students_updated.csv", header=header_ls, index=False)
Here, we saved our csv file with the headers as “students_updated.csv”. Also note that here, we are using
index=False to not add a separate column for the dataframe index in the csv file.
If we look at the contents of the csv file, you’ll see that the header is present.
The same can be confirmed by reading the csv file as a dataframe.
# read the updated csv file as dataframe df2 = pd.read_csv("students_updated.csv") # display the dataframe print(df2)
Age Name Gender 0 14 Tony Male 1 15 Emma Female 2 14 Rishi Male 3 16 Zara Female
The header is present.
Alternatively, you can use the
csv module, which is a built-in Python library that lets you work with csv files. Use the following steps to add a header using the
- Import the
- Read the contents of the csv file using the
csv.reader()method and convert it to a list.
- Add the headers to use as the first row in the above list.
- Save the above list as a csv file using the
Let’s take the same example as above and use the “students.csv” file which does not have a header.
import csv # read the csv file with open("students.csv", "r") as csv_file: # read the contents of the file reader = csv.reader(csv_file) # convert to list data = list(reader) # add the header to the list created above header_ls = ["Age", "Name", "Gender"] data.insert(0, header_ls) # write the data to a csv file with open("students_updated2.csv", "w") as csv_file: # writer object writer = csv.writer(csv_file) # write data to the csv file writer.writerows(data)
If you now check the contents of the “students_updated2.csv” file, it’ll have the given header.
You might also be interested in –
- Read CSV file as NumPy Array
- Pandas – Append dataframe to existing CSV
- Pandas – Read only the first n rows of a CSV file
- Save Pandas DataFrame to a CSV file
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.