Error when using pandas read_excel(header=[0,1])

前端 未结 1 712
故里飘歌
故里飘歌 2021-01-17 22:38

I\'m trying to use pandas read_excel to work with a file. The file has two columns of headers so I\'m trying to use the multiIndex feature apart of the header keyword argume

相关标签:
1条回答
  • 2021-01-17 23:27

    I could be mistaken but I don't think pandas handles parsing excel rows where there are merged cells. So in that first row, the merged cells get parsed as mostly empty cells. You'd need them nicely repeated to act correctly. This is what motivates the ffill below. If you could control the Excel workbook ahead of time and you might be able to use the code you have.


    my solution

    It's not pretty, but it'll get it done.

    filename = 'MOR-JANUARY 2015.xlsx'
    df1 = pd.read_excel(filename, sheetname='MOR', header=None)
    
    vals = df1.values
    
    mux = pd.MultiIndex.from_arrays(df1.ffill(1).values[:2, 1:], names=[None, 'DATE'])
    
    df1 = pd.DataFrame(df1.values[2:, 1:], df1.values[2:, 0], mux)
    
    0 讨论(0)
提交回复
热议问题