Read a CSV file without a header in Pandas

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.

Arguments:

  • 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

Advertisements

Preparing DataSet

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)

Output

  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.

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.

Output

         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)

Output

      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.

Summary

In this article, we have discussed how to read a CSV file without a header in Pandas.

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