所以我的数据框看起来像这样:
STORE PRODUCT INVENTORY
1 store1 a 1
2 store1 b 0
3 store2 a 0
4 store2 b 0
5 store3 a 1
6 store3 b 1
我想对此进行过滤,使其仅向我显示总库存 > 0
的商店,因为 store2
的总库存如下,结果如下库存为0。
STORE PRODUCT INVENTORY
1 store1 a 1
2 store1 b 0
5 store3 a 1
6 store3 b 1
我该怎么做?
最佳答案
你可以尝试:
df.loc[(df.groupby('STORE')['INVENTORY'].transform(sum) > 0)]
STORE PRODUCT INVENTORY
1 store1 a 1
2 store1 b 0
5 store3 a 1
6 store3 b 1
关于python - 通过聚合函数过滤 Pandas 数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74437634/