在 Pandas 中将两个 MultiIndex 级别合并为一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41987743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:53:54  来源:igfitidea点击:

Merge two MultiIndex levels into one in Pandas

pythonpandasmulti-index

提问by Anmol Singh

I have a Pandas data frame which is MultiIndexed. The second level contains a year ([2014,2015]) and the third contains the month number ([1, 2, .., 12]). I would like to merge these two into a single level like - [1/2014, 2/2014 ..., 6/2015]. How could this be done?

我有一个多索引的 Pandas 数据框。第二层包含年份 ([2014,2015]),第三层包含月份编号 ([1, 2, .., 12])。我想将这两个合并为一个级别,例如 - [1/2014, 2/2014 ..., 6/2015]。这怎么可能?

I'm new to Pandas. Searched a lot but could not find any similar question/solution.

我是Pandas的新手。搜索了很多,但找不到任何类似的问题/解决方案。

Edit: I found a way to avoid having to do this altogether with the answer to this question. I should have been creating my data frame that way. This seems to be the way to go for indexing by DateTime.

编辑:我找到了一种方法来避免在回答这个问题时完全这样做。我应该以这种方式创建我的数据框。这似乎是通过 DateTime 进行索引的方法。

回答by piRSquared

Consider the pd.MultiIndexand pd.DataFrame, muxand df

考虑pd.MultiIndexpd.DataFramemuxdf

mux = pd.MultiIndex.from_product([list('ab'), [2014, 2015], range(1, 3)])

df = pd.DataFrame(dict(A=1), mux)

print(df)

          A
a 2014 1  1
       2  1
  2015 1  1
       2  1
b 2014 1  1
       2  1
  2015 1  1
       2  1


We want to reassign to the index a list if lists that represent the index we want.

如果列表代表我们想要的索引,我们想为索引重新分配一个列表。

  • I want the 1st level the same

    df.index.get_level_values(0)
    
  • I want the new 2nd level to be a string concatenation of the current 2nd and 3rd levels but reverse the order

    df.index.map('{0[2]}/{0[1]}'.format)
    
  • 我想要第 1 级相同

    df.index.get_level_values(0)
    
  • 我希望新的第二级是当前第二级和第三级的字符串连接,但顺序相反

    df.index.map('{0[2]}/{0[1]}'.format)
    


df.index = [df.index.get_level_values(0), df.index.map('{0[2]}/{0[1]}'.format)]

print(df)

          A
a 1/2014  1
  2/2014  1
  1/2015  1
  2/2015  1
b 1/2014  1
  2/2014  1
  1/2015  1
  2/2015  1