简体   繁体   English

熊猫使用groupby为groupby变量的每个值应用不同的功能

[英]Pandas use groupby to apply a different function for each value of the groupby variable

I'd like to use groupby, but instead of applying the same functions to each group, I want to specify which function to apply to which group value. 我想使用groupby,但是我不想指定将相同的功能应用于每个组,而是要指定将哪个功能应用于哪个组值。 I'm providing a very simple example here to illustrate the point, but in reality there are many values of my groupby variable, and my functions are all user-defined and fairly complex -- so solutions that involve selecting each group separately or apply the same functions to all groups will not be practical. 我在这里提供了一个非常简单的示例来说明这一点,但实际上我的groupby变量有很多值,而且我的函数都是用户定义的并且相当复杂-因此,解决方案涉及分别选择每个组或应用对所有小组都使用相同的功能将是不切实际的。 (Answers of that sort were provided to this very similar question: how to apply different functions to each group of pandas groupby? but they don't address my question) (这种回答方式被提供给了这个非常相似的问题: 如何将不同的功能应用于每组大熊猫groupby?但是它们没有解决我的问题)

df = DataFrame({'Category': ['A','A','A','B','B','B','C','C','C'],
               'Total': [1, 2, 3, 1, 2, 3, 1, 2, 3]})

I'd like to be able to specify a function for each level of my groupby variable: 我希望能够为groupby变量的每个级别指定一个函数:

function_map = {'A': np.mean,
                'B': np.max,
                'C': np.min}

What I would like to be able to do is something like this: 我想做的是这样的:

df.groupby('Category').apply(function_map)

And the form of result I want would look like this DataFrame: 我想要的结果形式将类似于以下DataFrame:

result = DataFrame({'Category': ['A','B','C'],
               'Total': [2, 3, 1]})

just use a lambda, something like this 只是使用lambda,像这样

df.groupby('Category').apply(lambda r: function_map[r.name](r.Total))

also, you should use numpy functions so np.mean , np.max , np.min 另外,您应该使用numpy函数,因此np.meannp.maxnp.min

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM