Python Pandas does not read the first row of csv file Python Pandas does not read the first row of csv file python python

Python Pandas does not read the first row of csv file


By default, pd.read_csv uses header=0 (when the names parameter is also not specified) which means the first (i.e. 0th-indexed) line is interpreted as column names.

If your data has no header, then use

pd.read_csv(..., header=None)

For example,

import ioimport sysimport pandas as pdif sys.version_info.major == 3:    # Python3    StringIO = io.StringIO else:    # Python2    StringIO = io.BytesIOtext = '''\1 2 34 5 6'''print(pd.read_csv(StringIO(text), sep=' '))

Without header, the first line, 1 2 3, sets the column names:

   1  2  30  4  5  6

With header=None, the first line is treated as data:

print(pd.read_csv(StringIO(text), sep=' ', header=None))

prints

   0  1  20  1  2  31  4  5  6


If your file doesn't have a header row you need to tell Pandas so by using header=None in your call to pd.read_csv().