繁体   English   中英

pandas 滤波器组由

[英]pandas filter group by

我想过滤此查询中的结果,因此如果可能在一行代码中,表中只有结果 >1。

import pandas as pd 
import numpy as np 
df= pd.DataFrame({'Product':['A','B', 'C','A','B','D'],
                  'Age':[28,39,21,50,35,43], 
                  'Country':['USA','India','Germany','USA','India','India']
                 })
print(df.head())
table=df.groupby(['Product','Country'])['Age'].count()
table
import pandas as pd 
import numpy as np 
df= pd.DataFrame({'Product':['A','B', 'C','A','B','D'],
                  'Age':[28,39,21,50,35,43], 
                  'Country':['USA','India','Germany','USA','India','India']
                 })

table=df.groupby(['Product','Country'])['Age'].count().reset_index(name='count')
table1 = table[table["count"]>1]
table1

您可以像这样过滤计数列 fe。 您还可以将其更改为单行,例如:

table=df.groupby(['Product','Country'])['Age'].count().reset_index(name='count')[table["count"]>1]

让我们链式query方法:

table = (df.groupby(['Product','Country'])['Age'].count()
           .reset_index(name='Count')
           .query('Count > 1'))
table

Output:

  Product Country  Count
0       A     USA      2
1       B   India      2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM