How to read a csv file with multiple header rows into pandas?
16,702
You need parameter header=[0,1]
in read_csv
.
Author by
Caribgirl
Updated on June 05, 2022Comments
-
Caribgirl almost 2 years
I am not sure how to read the multiple rows. Do i have to do it without the rows and then do it manually in pandas? Or is there a way to read the whole csv file into pandas?
This is how the file looks Poverty data
-
Caribgirl almost 7 yearsI tried that then this error came up "Error tokenizing data. C error: Expected 1 fields in line 107, saw 3"
-
jezrael almost 7 yearsIt seems there are bad data, can you check line 107? Or is possible remove all problematic rows?
-
jezrael almost 7 yearsIf need remove problematic rows need error_bad_lines=False
-
Trisa Biswas over 5 yearsI tried doing this, but it is taking my first column as the index. How do I fix it?
-
jezrael over 5 years@TRISABISWAS - It is pain, mainly it depends of data, try
index_col=[0]
-
Trisa Biswas over 5 yearsindex_col=[0] and index_col=None both are giving me same result.
-
Trisa Biswas over 5 yearsI did a workaround by adding an index column in my excel sheet. Please let me know if there is any other efficient way to achieve this using pandas. Thank you.
-
jezrael over 5 years@TRISABISWAS - really hard way, because it depends of data, so find working solution is problem.... I have no another idea like
index_col=[0]
, so sorry :(