Prevent read_csv pandas from treating the first row as column headings

I read in pandas DataFrame using pd.read_csv . I want to save the first row as data, however it is converted to column names.

  • I tried header=False , but that completely removed it.

(Pay attention to my input: I have a line ( st = '\n'.join(lst) ) that I will convert to a file-like object ( io.StringIO(st) ) and then create csv from this file object. )

+10
python pandas csv
source share
2 answers

You want header=None False gets the type pushed to int at 0 , see docs :

header: int or list of ints, default 'infer The number of rows to use as column names and the start of the data. The default behavior looks like it is set to 0 if the names are not passed, otherwise None . Explicitly pass header = 0 to be able to replace existing names. The header can be a list of integers indicating the location of the rows for the multi-index on the columns, for example [0,1,3]. Intermediate lines that are not specified will be skipped (for example, 2 is skipped in this example). Note that this parameter ignores commented lines and empty lines if skip_blank_lines = True, so header = 0 indicates the first line of data, and not the first line of the file.

You can see the difference in behavior, first with header=0 :

 In [95]: import io import pandas as pd t="""a,b,c 0,1,2 3,4,5""" pd.read_csv(io.StringIO(t), header=0) Out[95]: abc 0 0 1 2 1 3 4 5 

Now with None :

 In [96]: pd.read_csv(io.StringIO(t), header=None) Out[96]: 0 1 2 0 abc 1 0 1 2 2 3 4 5 

Note that in the latest version 0.19.1 will now be displayed:

 In [98]: pd.read_csv(io.StringIO(t), header=False) 

TypeError: passing a bool header is not allowed. Use header = None for no header or header = int or list-like from int to specify the row (s) creating the column names

+17
source share

It seems to me that you need the header=None parameter for read_csv :

Example:

 import pandas as pd from pandas.compat import StringIO temp=u"""a,b 2,1 1,1""" df = pd.read_csv(StringIO(temp),header=None) print (df) 0 1 0 ab 1 2 1 2 1 1 
+6
source share

All Articles