In this article, we will discuss how to read a CSV file without a header in Pandas.
Python panda’s library provides a read_csv function to read the pandas DataFrame with varied customizations.
pandas.read_csv(filepath_or_buffer, header, names, ....)
It accepts a few more arguments as well but here we will discuss a few important arguments only i.e.
- filepath_or_buffer : string containing CSV filepath with filename
- headers : Row number to use as column headers (default is the first row)
- names : List of column headers
Table of Contents
To quickly get started, we will use a sample CSV without any headers. The contents of the CSV data are as below
Shubham,India,Tech India,5 Riti,India,India,7 Shanky,India,PMO,2 Shreya,India,Design,2 Aadi,US,Tech,11 Sim,US,Tech,4
There are no headers in the above CSV file, so if we directly read using the pandas.read_csv(), it will set the first row as the column header, which is incorrect (shown below).
import pandas as pd # read the CSV file df = pd.read_csv("file.csv") print (df)
Shubham India Tech India 5 0 Riti India India 7 1 Shanky India PMO 2 2 Shreya India Design 2 3 Aadi US Tech 11 4 Sim US Tech 4
Reading CSV without column headers
To read a CSV file without a header, we will need to provide the header attribute as “None”, meaning we don’t want to use any of the rows as the column headers. Let’s try to read the file again.
Latest Python - Video Tutorial
import pandas as pd # read the CSV file without headers df = pd.read_csv("file.csv", header = None) print (df)
The output of the file looks as below.
0 1 2 3 0 Shubham India Tech India 5 1 Riti India India 7 2 Shanky India PMO 2 3 Shreya India Design 2 4 Aadi US Tech 11 5 Sim US Tech 4
As observed, the pandas.read_csv function has set default column headers instead of using the first row from our data.
Setting the headers while reading
Instead of these default header values, we can also provide our choice of column headers while reading. We will set using the “names” attribute from the pandas.read_csv function.
import pandas as pd # read the CSV file without headers and set the column names df = pd.read_csv("file.csv", header=None, names = ['Name', 'Location', 'Team', 'Experience']) print (df)
Name Location Team Experience 0 Shubham India Tech India 5 1 Riti India India 7 2 Shanky India PMO 2 3 Shreya India Design 2 4 Aadi US Tech 11 5 Sim US Tech 4
As observed, we have successfully read the CSV without headers and have also set custom column headers while reading only.
In this article, we have discussed how to read a CSV file without a header in Pandas.
Latest Video Tutorials