How to read Pandas csv file with no header

+1 vote
I have a csv file that I am importing in my Python script using pandas. The csv file start with cell values and doesn’t contain headings. Pandas is considering the first row value as heading. How to read csv without heading?
Apr 4, 2019 in Python by Raj
230,419 views

5 answers to this question.

0 votes

You can set the header option to None to ignore header.

file = pd.read_csv('Diabetes.csv', header=1)
answered Apr 4, 2019 by Shri
file = pd.read_csv('Diabetes.csv', header=None) 
We need to keep header as None instead of 1
+1 vote

Use this logic, if header is present but you don't want to read.

Using only header option, will either make header as data or one of the data as header. So, better to use it with skiprows, this will create default header (1,2,3,4..) and remove the actual header of file.

dfE_NoH = pd.read_csv('example.csv',header = 1)     

42 Jason Miller 25,000 4
0 52 Molly Jacobson 94,000 24
1 36 Tina . 57 31
2 24 Jake Milner 62 .

dfE_NoH = pd.read_csv('example.csv',header = None)

0 1 2 3 4
0 age first_name last_name postTestScore preTestScore
1 42 Jason Miller 25,000 4
2 52 Molly Jacobson 94,000 24
3 36 Tina . 57 31

df = pd.read_csv('example.csv', skiprows = 1,header = None)   

0 1 2 3 4
0 42 Jason Miller 25,000 4
1 52 Molly Jacobson 94,000 24
2 36 Tina . 57 31
3 24 Jake Milner 62 .
answered Mar 14, 2020 by Shahabuddin
• 160 points
0 votes

In order to read a csv in that doesn't have a header and for only certain columns you need to pass params header=None and usecols=[3,6] for the 4th and 7th columns:

df = pd.read_csv(file_path, header=None, usecols=[3,6])
answered Dec 11, 2020 by Gitika
• 65,770 points
0 votes

Load CSV files to Python Pandas

  1. Load the Pandas libraries with alias 'pd'
  2. import pandas as pd.
  3. Read data from file 'filename.csv'
  4. # (in the same directory that your python process is based)
  5. # Control delimiters, rows, column names with read_csv (see later)
  6. data = pd. ...
  7. # Preview the first 5 lines of the loaded data.
answered Dec 11, 2020 by Rajiv
• 8,870 points
0 votes

Reading in a .csv file into a Pandas DataFrame will by default, set the first row of the .csv file as the headers in the table. However, if the .csv file does not have any pre-existing headers, Pandas can skip this step and instead start reading the first row of the .csv as data entries into the data frame.

USE pandas.io.parsers.read_csv() TO READ IN A .csv FILE WITHOUT HEADERS

Call pandas.read_csv(file, header = None) with file set to the name of the .csv to be read into the DataFrame.

SAMPLE.CSV

1,2
3,4
df = pd.read_csv('sample.csv', header=None)


print(df)

OUTPUT

   0  1
0  1  2
1  3  4
answered Dec 11, 2020 by Roshni
• 10,480 points

Related Questions In Python

0 votes
2 answers

How do I convert text file to CSV file with only plain python. Meaning no panda or any other special module?

Steps to Convert Text File to CSV ...READ MORE

answered Oct 15, 2020 in Python by Abdul Shekh
9,232 views
0 votes
1 answer

How to read a HDF file with Pandas?

Hi@akhtar, You can read an HDF file using ...READ MORE

answered Oct 18, 2020 in Python by MD
• 95,460 points
1,978 views
0 votes
1 answer

How to create blank csv file in pandas?

Try this code: open("User data.csv", 'w') Where User data.csv ...READ MORE

answered Apr 6, 2019 in Python by Will
4,468 views
0 votes
2 answers

How to check if a csv file is empty in pandas?

Try this: df = pd.DataFrame(columns=['Name', 'ID', 'Department']) if df.empty ...READ MORE

answered Jul 1, 2019 in Python by Bob
14,734 views
–1 vote
2 answers
0 votes
1 answer

How to convert CSV file to JSON file using Pandas?

Hi@akhtar, You can convert your CSV file to ...READ MORE

answered Jun 25, 2020 in Python by MD
• 95,460 points
11,381 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,067 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP