简体   繁体   中英

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.

Appreciate any help. Thank you

You need len(group) for size of groups :

db=table.groupby(['Type','Quarter'])
        .filter(lambda group: len(group) > 3)
        .groupby(['Type','Quarter'])["Price"]
        .mean()

Or use transform :

db=table[table.groupby(['Type','Quarter'])['Type'].transform('size') > 3]
           .groupby(['Type','Quarter'])["Price"].mean()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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