【问题标题】:Read multi-index excel file and reshape the headers in Pandas读取多索引 excel 文件并重塑 Pandas 中的标题
【发布时间】:2020-08-06 10:31:50
【问题描述】:

给定一个excel文件data.xlsx如下:

我用df = pd.read_excel('data.xlsx', header = [0, 1], index_col = [0, 1], sheet_name = 'Sheet1')读过,

输出:

district  2018        2019      
         price ratio price ratio
bj cy       12  0.01     6  0.02
sh hp        4  0.02     3  0.05

我想知道是否可以将其转换为以下格式?感谢您的帮助。

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    DataFrame.stackDataFrame.rename_axisDataFrame.reset_index 一起使用:

    df = df.stack(0).rename_axis(('city','district','year')).reset_index()
    print (df)
      city district  year  price  ratio
    0   bj       cy  2018     12   0.01
    1   bj       cy  2019      6   0.02
    2   sh       hp  2018      4   0.02
    3   sh       hp  2019      3   0.05
    

    【讨论】:

    • 谢谢。我是否正确阅读了excel?似乎city 不见了。
    • 您可以从问题中检查pd.read_excel('data.xlsx', header = [0, 1], index_col = [0, 1], sheet_name = 'Sheet1') 的输出,没有列名city
    • @ahbon - 我明白,它只是元数据等列的名称。因此,如果不在输入文件中,则无法解析。所以在我的解决方案中使用rename_axis 进行设置。
    • @ahbon - this 工作怎么样?
    • 谢谢,我去测试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2016-10-28
    • 1970-01-01
    • 2014-07-31
    • 2021-02-24
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多