How to Read a CSV file Without Headers in Pandas?

Spread the love

Problem –

You have a csv file which does not have headers and you want to read it.

Solution –

Let’s say we have a csv file which does not have headers as shown below.

To read this csv file we can use the header parameter in read_csv method. All we have to do is set header=None.

import pandas as pd

df = pd.read_csv('./Restaurant.csv', header=None)
df.head()

We can see that we have successfully read the csv file. And as we have no headers the column names are 0, 1, 2 at this moment. If you want to add column names when reading the csv file, you can use the names parameter like this.

import pandas as pd

df = pd.read_csv('./Restaurant.csv', header=None, names=['Restaurant','Quality','Meal Price'])
df.head()

Rating: 1 out of 5.

Leave a Reply