简体   繁体   中英

Drop pandas dataframe rows based on groupby() condition

There is a pandas dataframe on input:

store_id item_id  items_sold        date
1          1          0        2015-12-28
1          2          1        2015-12-28
1          1          0        2015-12-28
2          2          0        2015-12-28
2          1          1        2015-12-29
2          2          1        2015-12-29
2          1          0        2015-12-29
3          1          0        2015-12-30
3          1          0        2015-12-30

is that what you want?

In [30]: df[df.groupby(['store_id', 'item_id'])['items_sold'].transform('sum') > 0]
Out[30]:
   store_id  item_id  items_sold        date
1         1        2           1  2015-12-28
3         2        2           0  2015-12-28
4         2        1           1  2015-12-29
5         2        2           1  2015-12-29
6         2        1           0  2015-12-29

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