简体   繁体   中英

Pandas group by filter based on conditions

I have a dataset quite similar to mentioned here http://pandas.pydata.org/pandas-docs/stable/10min.html#grouping

>>> df
     A      B         C
0  foo    one -1.735400
1  bar    one -0.148954
2  foo    two  0.103798
3  bar  three -0.576249
4  foo    two  1.379046
5  bar    two  0.802281
6  foo    one -0.758771
7  foo  three  1.270179

I have executed command as df.groupby(['A','B'])['C'].sum()

>>> grpd = df.groupby(['A','B'])['C'].sum()

>>> grpd
A    B    
bar  one     -0.148954
     three   -0.576249
     two      0.802281
foo  one     -2.494171
     three    1.270179
     two      1.482844
Name: C, dtype: float64

In my particular case, Column A has storeid, B is month (over year) and C is sales value

Goal is to find All storeid who have sold beyond a certain value for each month.

Example question - Which storied have sold beyond $1000 for any month?

I can easily do this in SQL but not sure how to do it in Pandas data frame.

如果我理解正确的话,你可以groupby的MuliIndex水平,然后用过滤。

grpd.groupby(level='A').filter(lambda grp: (grp > 1000).all())

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