Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas read in table without headers

Tags:

python

pandas

How can I read in a .csv file (with no headers) and when I only want a subset of the columns (say 4th and 7th out of a total of 20 columns), using pandas? I cannot seem to be able to do usecols

like image 363
user308827 Avatar asked Mar 26 '15 19:03

user308827


People also ask

How do I remove a header from a data frame?

In order to export pandas DataFrame to CSV without index (no row indices) use param index=False and to ignore/remove header use header=False param on to_csv() method.

What is header in read_csv?

header: this allows you to specify which row will be used as column names for your dataframe. Expected an int value or a list of int values. Default value is header=0 , which means the first row of the CSV file will be treated as column names. If your file doesn't have a header, simply set header=None .


1 Answers

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]) 

See the docs

like image 77
EdChum Avatar answered Sep 18 '22 04:09

EdChum