How To Read a CSV file Without a Header in Pandas – Definitive Guide

Some CSV files might not have headers in them.

You can read csv file without a header in pandas using the read_csv() method and header=none parameter.

This tutorial teaches you how to read a CSV file without a header in Pandas.

If You’re in Hurry…

Use the following code to read the CSV file without a header and set column names manually.

import pandas as pd

df = pd.read_csv('addresses.csv', header=None, names=['First Name', 'Last Name', 'Address', 'State', 'Zip Code'])

df

If You Want to Understand Details, Read on…

Pandas read_csv() reads the first row as a header by default. If the CSV file does not contain a header, you need to pass the header=None to tell pandas that the first row is also a data row.

Using ReadCSV And Header=None

By default, the pandas read_csv() method considers the first row of the CSV file as the header.

  • To read a CSV file without a header in Pandas, you need to pass the header=None parameter.
  • This parameter denotes that the first line in the CSV file is also a data row, not a header row.

Code

The following code demonstrates how to use the header=None parameter in the read_csv() method.

import pandas as pd

df = pd.read_csv('addresses.csv', header=None)

df

Dataframe Will Look Like

012345
0JohnDoe120 jefferson st.RiversideNJ8075
1JackMcGinnis220 hobo Av.PhilaPA9119
2John “Da Man”Repici120 Jefferson St.RiversideNJ8075
3StephenTyler7452 Terrace “At the Plaza” roadSomeTownSD91234
4NaNBlankmanNaNSomeTownSD298
5Joan “the bone”, AnneJet9th, at Terrace plcDesert CityCO123

Read CSV Without Header and Set Column Names

To read a CSV file without a header and set column names, you can use the header=None parameter and pass the column headers using the names parameter.

You can pass the columns as a list to the names parameter. For example, names = [‘Col 1’, ‘Col 2’] and so on.

Code

The following code demonstrates how to set the column names while reading the CSV file.

import pandas as pd

df = pd.read_csv('addresses.csv', header=None, names=['First Name', 'Last Name', 'Address', 'State', 'Zip Code'])

df

Dataframe Will Look Like

First NameLast NameAddressStateZip Code
JohnDoe120 jefferson st.RiversideNJ8075
JackMcGinnis220 hobo Av.PhilaPA9119
John “Da Man”Repici120 Jefferson St.RiversideNJ8075
StephenTyler7452 Terrace “At the Plaza” roadSomeTownSD91234
NaNBlankmanNaNSomeTownSD298
Joan “the bone”, AnneJet9th, at Terrace plcDesert CityCO123

Read Specific Columns From CSV Without Header

To read specific columns from CSV, use the usecols parameter and pass the index of the columns as a list.

  • It reads the columns specified in the list
  • Uses the column names passed in the names parameter.

Code

The following code demonstrates how to read the columns with index 1 and 2 from the CSV file without a header.

import pandas as pd

df = pd.read_csv('addresses.csv', header=None, usecols=[1,2], names=['First Name', 'Last Name'])

df

Dataframe Will Look Like

First NameLast Name
0Doe120 jefferson st.
1McGinnis220 hobo Av.
2Repici120 Jefferson St.
3Tyler7452 Terrace “At the Plaza” road
4BlankmanNaN
5Jet9th, at Terrace plc

Additional Resources

Leave a Comment