问题描述
使用groupby并同时在熊猫中应用过滤器的最有效方法是什么?
what would be the most efficient way to use groupby and in parallel apply a filter in pandas?
我基本上要求的是SQL中的等价物
Basically I am asking for the equivalent in SQL of
select *
...
group by col_name
having condition
我认为有很多用例,包括条件均值,总和,条件概率等,这些使这种命令非常强大.
I think there are many uses cases ranging from conditional means, sums, conditional probabilities, etc. which would make such a command very powerful.
我需要一个非常好的性能,因此理想情况下,这样的命令将不是在python中进行多次分层操作的结果.
I need a very good performance, so ideally such a command would not be the result of several layered operations done in python.
推荐答案
如unutbu的评论所述, groupby的过滤器等同于SQL的HAVING:
As mentioned in unutbu's comment, groupby's filter is the equivalent of SQL'S HAVING:
In [11]: df = pd.DataFrame([[1, 2], [1, 3], [5, 6]], columns=['A', 'B'])
In [12]: df
Out[12]:
A B
0 1 2
1 1 3
2 5 6
In [13]: g = df.groupby('A') # GROUP BY A
In [14]: g.filter(lambda x: len(x) > 1) # HAVING COUNT(*) > 1
Out[14]:
A B
0 1 2
1 1 3
您可以编写更复杂的函数(将这些函数应用于每个组),前提是它们返回简单的布尔值:
You can write more complicated functions (these are applied to each group), provided they return a plain ol' bool:
In [15]: g.filter(lambda x: x['B'].sum() == 5)
Out[15]:
A B
0 1 2
1 1 3
注意:可能存在一个错误,您无法编写可对之采取行动的函数您用于分组的列...一种解决方法是手动对列进行分组,即g = df.groupby(df['A']))
.
Note: potentially there is a bug where you can't write you function to act on the columns you've used to groupby... a workaround is the groupby the columns manually i.e. g = df.groupby(df['A']))
.
这篇关于SQL的“按组分组"的含义是什么?在 pandas 上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!