Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于数据帧中的计数从数据帧中排除组_Python_Pandas - Fatal编程技术网

Python 基于数据帧中的计数从数据帧中排除组

Python 基于数据帧中的计数从数据帧中排除组,python,pandas,Python,Pandas,我有一个数据框df,其中我根据foll计算了每组的数量。代码: df[['作物类型','收获季节]].groupby(['作物类型','收获季节]]).agg(['count']) 此代码的输出是上面的数据帧 如何从计数小于30的原始数据帧中排除组?您可以更改逻辑-如果组的计数大于或等于30,请按in选择所有行,以便在系列中重复聚合计数,并使用与原始数据帧相同的大小,因此可能通过以下方式进行过滤: 这回答了你的问题吗?如果您查看第一个答案的多个条件部分,只需使用其中的一半检查

我有一个数据框
df
,其中我根据foll计算了每组的数量。代码:
df[['作物类型','收获季节]].groupby(['作物类型','收获季节]]).agg(['count'])

此代码的输出是上面的数据帧


如何从计数小于30的原始数据帧中排除组?

您可以更改逻辑-如果组的计数大于或等于
30
,请按in选择所有行,以便在
系列中重复聚合计数,并使用与原始
数据帧
相同的大小,因此可能通过以下方式进行过滤:


这回答了你的问题吗?如果您查看第一个答案的多个条件部分,只需使用其中的一半检查<30否它不回答,我正在使用groupby操作的计数结果从原始DataFrameThank中排除行!为什么你做了
df[df.groupby(['Crop Type','Harvest seasure'])['Crop Type'].transform('count').ge(30)]
df[df.groupby(['Crop Type','Harvest seasure'])['Crop Type','Harvest seasure'].transform('count').ge(30)]
@user308827-因为如果使用
df groupby(['Crop Type','Harvest seasure'])['Crop Type','Harvest seash'])['.transform('count')
获取输出2列数据帧,因为只需要使用过滤器
系列
。因此,如果使用
df.groupby(['Crop Type','Harvest seasure'])['Crop Type','Harvest seasure'].transform('count').ge(30)
获取具有行中相同值的布尔数据帧(如果没有丢失数据,因为
count
排除它们)
                             Attribute
                                 count
Crop Type     Harvest Season
Barley        Spring                25
Corn (Grain)  Spring               655
              Winter                 1
Corn (Silage) Spring                 6
Cotton        Spring                 5
Peas          Spring                 3
Canola        Spring               169
              Winter               164
Soybeans      Spring               541
              Winter                 2
Sugar beet    Spring                82
Sunflower     Spring               637
              Winter                 1
Wheat         Spring               253
              Winter               451
df[df.groupby(['Crop Type', 'Harvest Season'])['Crop Type'].transform('count').ge(30)]