代码之家  ›  专栏  ›  技术社区  ›  Michael

读取包含空单元格的Excel文件作为多索引Pandas数据框

  •  0
  • Michael  · 技术社区  · 5 年前

    假设有一个excel文件:

    enter image description here

    有没有一种方法可以将其直接读取为具有多索引的pandas数据帧,而不在第一列中填充空格?

    1 回复  |  直到 5 年前
        1
  •  2
  •   Trenton McKinney ivirshup    5 年前

    数据:

    enter image description here

    代码:

    df = pd.read_excel('test.xlsx')
    

    enter image description here

    .ffill() :

    df.i0.ffill(inplace=True)
    

    enter image description here

    set_index() :

    df.set_index(['i0', 'i1'], inplace=True)
    

    enter image description here