本文介绍了 pandas :获得系列的多索引水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

              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.

我尝试了df.index.levels[1],但是会创建一个排序列表,但不会保留顺序.

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

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

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

如果在其他地方解决了此问题,请分享,因为我没有运气来搜索stackoverflow存档.

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

推荐答案

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

这篇关于 pandas :获得系列的多索引水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:38