[英]Python pandas dataframe group mean filtered by condition
I'd like to apply the command below to a data frame where the number of groups meet a minimum count criteria. 我想将下面的命令应用于其中组数满足最小计数标准的数据框。
db=table.groupby(['Type','Quarter'])["Price"].mean()
So far the below example isn't returning the needed results. 到目前为止,以下示例尚未返回所需的结果。
db=table.groupby(['Type','Quarter']).filter(lambda group: group.size > 3).groupby(['Type','Quarter'])["SALE_PRC"].mean()
Basically I'd like to find the mean of the["Price"] for the (['Type','Quarter']) groups but only if the number of records exceeds 3. 基本上,我只想找到(['Type','Quarter'])组的[“ Price”]的平均值,但前提是记录数超过3。
Appreciate any help. 感谢任何帮助。 Thank you 谢谢
You need len(group)
for size of groups
: 您需要len(group)
来确定groups
大小:
db=table.groupby(['Type','Quarter'])
.filter(lambda group: len(group) > 3)
.groupby(['Type','Quarter'])["Price"]
.mean()
Or use transform
: 或使用transform
:
db=table[table.groupby(['Type','Quarter'])['Type'].transform('size') > 3]
.groupby(['Type','Quarter'])["Price"].mean()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.