【问题标题】:Changing MultiIndex Headers List to Array将 MultiIndex 标头列表更改为数组
【发布时间】:2018-05-07 14:08:39
【问题描述】:

我希望根据多个标题条件查询信息。在读取具有 6 个标题行的文件时,看起来标题被读取为 1 个大列表而不是 6 个数组。为了使用多索引函数,我需要转换为数组吗?这是怎么做到的?

data = pd.read_csv('data.csv', header = [0,1,2,3,4,5], index_col = 0)
list(data)

[('US Banks', 'Main', 'Sectors', 'ALL', 'ALL', 'Modified Duration'),
 ('US Banks', 'Main', 'Sectors', 'ALL', '1-3', 'Modified Duration'),
 ('US Banks', 'Main', 'Sectors', 'ALL', '3-5', 'Modified Duration'),
 ('US Banks', 'Main', 'Sectors', 'ALL', '5-7', 'Modified Duration'),
 ('US Banks', 'Main', 'Sectors', 'ALL', '7-10', 'Modified Duration'),
 ('US Banks', 'Main', 'Sectors', 'ALL', '10+', 'Modified Duration'),...]

【问题讨论】:

    标签: python pandas dataframe multi-index


    【解决方案1】:

    尝试使用pd.MultiIndex.from_tuples

    data.columns=pd.MultiIndex.from_tuples(list(data))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2016-09-19
      • 2018-02-01
      • 2015-03-12
      • 1970-01-01
      相关资源
      最近更新 更多