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.
Table of Contents
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
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | John | Doe | 120 jefferson st. | Riverside | NJ | 8075 |
1 | Jack | McGinnis | 220 hobo Av. | Phila | PA | 9119 |
2 | John “Da Man” | Repici | 120 Jefferson St. | Riverside | NJ | 8075 |
3 | Stephen | Tyler | 7452 Terrace “At the Plaza” road | SomeTown | SD | 91234 |
4 | NaN | Blankman | NaN | SomeTown | SD | 298 |
5 | Joan “the bone”, Anne | Jet | 9th, at Terrace plc | Desert City | CO | 123 |
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 Name | Last Name | Address | State | Zip Code | |
---|---|---|---|---|---|
John | Doe | 120 jefferson st. | Riverside | NJ | 8075 |
Jack | McGinnis | 220 hobo Av. | Phila | PA | 9119 |
John “Da Man” | Repici | 120 Jefferson St. | Riverside | NJ | 8075 |
Stephen | Tyler | 7452 Terrace “At the Plaza” road | SomeTown | SD | 91234 |
NaN | Blankman | NaN | SomeTown | SD | 298 |
Joan “the bone”, Anne | Jet | 9th, at Terrace plc | Desert City | CO | 123 |
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 Name | Last Name | |
---|---|---|
0 | Doe | 120 jefferson st. |
1 | McGinnis | 220 hobo Av. |
2 | Repici | 120 Jefferson St. |
3 | Tyler | 7452 Terrace “At the Plaza” road |
4 | Blankman | NaN |
5 | Jet | 9th, at Terrace plc |
Additional Resources
- How To Replace Header With First Row In Pandas Dataframe
- How to Remove the Header Row From Pandas Dataframe
- How to Read a CSV file From a URL in Pandas (With Authentication)
- How To Read Specific Columns From a CSV using Pandas Python
- How to Remove the Index Column in Pandas While Reading from the CSV file