pandas 如何按一级索引分组并将函数应用于熊猫中的二级索引

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

How to groupby the first level index and apply function to the second index in Pandas

pythonpandas

提问by bigbug

I have multilevel dataframe 'df' like this :

我有这样的多级数据框“df”:

             col1 col2
first second
a        0    5    5
         1    5    5
         2    5    5
b        0    5    5
         1    5    5

And I want to apply a function func(exp: 'lambda x: x*10') to second, somewhat like :

我想应用一个函数func(exp:)'lambda x: x*10'second,有点像:

df.groupby(level='first').second.apply(func)

and result will lokk like:

结果会像:

             col1 col2
first second
a        0    5    5
         10   5    5
         20   5    5
b        0    5    5
         10   5    5

The above command not work for secondis not a column, so .secondis not accepted by Pandas .

上述不适用的命令不是second列,因此.second不被 Pandas 接受。

I don't want to do that by df.reset_index(), blablabla..., then finally df.set_index().I prefer to do it in one command, How to do ?

我不想通过df.reset_index()blablabla做到这一点,最后df.set_index().我更喜欢用一个命令来做到这一点,怎么办?

采纳答案by Def_Os

When creating the DataFrame, you could set the MultiIndex as follows:

创建 DataFrame 时,您可以按如下方式设置 MultiIndex:

df.set_index(['first', 'second'], drop=False)

This way, the index column is not dropped and still accessible for your apply.

这样,索引列不会被删除并且您的apply.

回答by JoeCondron

You can use the set_levelsmethod of the index to change the values in a given level. So for a given funcand levelyou can do:

您可以使用set_levels索引的方法来更改给定级别中的值。所以对于给定的funclevel你可以这样做:

new_values = map(func, df.index.get_level_values(level))
df.index.set_levels(new_values, level, inplace=True)