简体   繁体   English

带有DataFrame的Pandas groupby系列

[英]Pandas groupby Series with DataFrame

I would like to group a Series by a DataFrame and then perform a reduction as in the following example: 我想按一个DataFrame对一个Series进行分组,然后按照以下示例进行归约:

In [1]: from pandas import DataFrame

In [2]: df = DataFrame([['Alice', 'F', 100, 1],
                        ['Alice', 'F', 100, 3],
                        ['Drew', 'F', 100, 4],
                        ['Drew', 'M', 100, 5],
                        ['Drew', 'M', 200, 5]],
                       columns=['name', 'sex', 'amount', 'id'])

In [3]: df['amount'].groupby(df[['name', 'sex']]).count()

Unfortunately this raises the following TypeError which has me stumped 不幸的是,这引发了下面的TypeError

TypeError: 'DataFrame' object is not callable

I know that I can use the column names directly but I my actual computation needs to be a bit more general than that and thought that this would be doable. 我知道我可以直接使用列名,但是我的实际计算需要比这更通用,并认为这是可行的。 What is going on here? 这里发生了什么? What is the proper way to group-and-reduce a series by an arbitrary DataFrame? 通过任意DataFrame对系列进行分组和归约的正确方法是什么? Or alternatively, does such a way not exist? 或者,这种方法不存在吗?

One solution is to turn the Series into a DataFrame, join to the grouper DataFrame, then groupby on the columns of the grouper then reselect out the columns of the grouped. 一种解决方案是将Series转换为DataFrame,加入到分组器DataFrame,然后在分组器的列上进行分组,然后重新选择分组的列。 Ie

# Example inputs
pregrouped = df['amount']
grouper = df[['name', 'sex']]

# General computation
pregrouped = DataFrame(pregrouped)
grouper = DataFrame(grouper) 

full = grouper.join(pregrouped)
groups = full.groupby(list(grouper.columns))[list(pregrouped.columns)]
result = groups.some_reduction()[list(pregrouped.columns)].reset_index()

Is anything here very wasteful? 这里有什么很浪费的吗? This approach runs at about the speed of the normal idiomatic computation that's available in common cases. 这种方法的运行速度大约与普通情况下惯用的惯用计算速度相同。

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

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