0

I have multi-level columns in a csv. How can I read them, while maintaining their multi-level hierarchy?

The contents of the csv are as follows:

name,code,code,date,date,approved
,min,max,min,max,
apple,4,7,20200115,20200724,yes
banana,2,8,20191106,20200809,no
carrot,5,6,20200323,20200617,yes
...

I would like to read them (preferably via pandas) such that the contents of the dataframe are as follows:

name     code         date               approved
         min   max    min      max
---      ---   ---    ---      ---       ---
apple    4     7      20200115 20200724  yes
banana   2     8      20191106 20200809  no
carrot   5     6      20200323 20200617  yes
...

I have tried the following:

import pandas as pd
df = pd.read_csv(filename, index_col=None)

The above code stores the following contents in df:

   name     code  code.1  date     date.1    approved
   ---      ---   ---     ---      ---       ---
0  NaN      min   max     min      max       NaN
1  apple    4     7       20200115 20200724  yes
2  banana   2     8       20191106 20200809  no
3  carrot   5     6       20200323 20200617  yes
...

which is not what I want. How can I store the contents in the dataframe as described above?

SaadH
  • 1,042
  • 2
  • 19
  • 32
  • 1
    did you try `header=[0, 1]`? I actually asked a similar question here: https://stackoverflow.com/questions/56729970/write-and-read-pandas-dataframes-with-multiindex-columns – Nakor Oct 09 '20 at 04:33
  • 1
    This might help you https://medium.com/@gotashirato/how-to-import-csv-file-with-multi-level-columns-python-basics-and-a-question-b67cbbbc174d – apoorva kamath Oct 09 '20 at 04:36

0 Answers0