numpy – Python Pandas does not read the first row of csv file

numpy – 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 io
import sys
import pandas as pd
if sys.version_info.major == 3:
    # Python3
    StringIO = io.StringIO 
else:
    # Python2
    StringIO = io.BytesIO

text = 
1 2 3
4 5 6


print(pd.read_csv(StringIO(text), sep= ))

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

   1  2  3
0  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  2
0  1  2  3
1  4  5  6

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

numpy – Python Pandas does not read the first row of csv file

Leave a Reply

Your email address will not be published. Required fields are marked *