Pandas DataFrame 删除 groupby 中的行 [英] Pandas DataFrame to drop rows in the groupby
问题描述
我有一个包含三列 Date
、Advertiser
和 ID 的 DataFrame.我首先对数据进行分组,以查看某些广告商的数量是否太小(例如,当 count()
小于 500 时).然后我想删除组表中的那些行.
I have a DataFrame with three columns Date
, Advertiser
and ID. I grouped the data firsts to see if volumns of some Advertisers are too small (For example when count()
less than 500). And then I want to drop those rows in the group table.
df.groupby(['Date','Advertiser']).ID.count()
结果是这样的:
Date Advertiser
2016-01 A 50000
B 50
C 4000
D 24000
2016-02 A 6800
B 7800
C 123
2016-03 B 1111
E 8600
F 500
我想要这样的结果:
Date Advertiser
2016-01 A 50000
C 4000
D 24000
2016-02 A 6800
B 7800
2016-03 B 1111
E 8600
后续问题:
如果我想根据日期类别中的总 count()
过滤掉 groupby 中的行如何.比如我要count()
一个大于15000的日期.我要的表是这样的:
How about if I want to filter out the rows in groupby in term of the total count()
in date category. For example, I want to count()
for a date larger than 15000. The table I want likes this:
Date Advertiser
2016-01 A 50000
B 50
C 4000
D 24000
2016-02 A 6800
B 7800
C 123
推荐答案
你在 groupby
后面有一个 Series 对象,可以用链式 lambda 根据值过滤> 过滤器:
You have a Series object after the groupby
, which can be filtered based on value with a chained lambda filter:
df.groupby(['Date','Advertiser']).ID.count()[lambda x: x >= 500]
#Date Advertiser
#2016-01 A 50000
# C 4000
# D 24000
#2016-02 A 6800
# B 7800
#2016-03 B 1111
# E 8600
# F 500
这篇关于Pandas DataFrame 删除 groupby 中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!