Pandas:将多索引级别作为系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39045948/
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 01:51:04  来源:igfitidea点击:

Pandas: get multiindex level as series

pythonpandasmulti-index

提问by A User

I have a dataframe with multiple levels, eg:

我有一个具有多个级别的数据框,例如:

idx = pd.MultiIndex.from_product((['foo', 'bar'], ['one', 'five', 'three' 'four']),
                                 names=['first', 'second'])
df = pd.DataFrame({'A': [np.nan, 12, np.nan, 11, 16, 12, 11, np.nan]}, index=idx).dropna().astype(int)

              A     
first second
foo   five     12
      four     11
bar   one      16
      five     12
      three    11

I want to create a new column using the index level titled second, so that I get

我想使用标题为 的索引级别创建一个新列second,以便我得到

              A    B  
first second
foo   five     12   five
      four     11   four
bar   one      16   one
      five     12   five
      three    11   three

I can do this by resetting the index, copying the column, then re-applying, but that seems more round-about.

我可以通过重置索引,复制列,然后重新应用来做到这一点,但这似乎更迂回。

I tried df.index.levels[1], but that creates a sorted list, it doesn't preserve the order.

我试过df.index.levels[1],但这会创建一个排序列表,它不会保留顺序。

If it was a single index, I would use df.indexbut in a multiindex that creates a column of tuples.

如果它是单个索引,我会df.index在创建一列元组的多索引中使用。

If this is resolved elsewhere, please share as I haven't had any luck searching the stackoverflow archives.

如果这在其他地方得到解决,请分享,因为我没有任何运气搜索 stackoverflow 档案。

回答by Alexander

df['B'] = df.index.get_level_values(level=1)  # Zero based indexing.
# df['B'] = df.index.get_level_values(level='second')  # This also works.
>>> df
               A      B
first second           
foo   one     12    one
      two     11    two
bar   one     16    one
      two     12    two
      three   11  three

回答by Alberto Garcia-Raboso

df['B'] = idx.to_series().str[1]