熊猫过滤组依据



我想过滤这个查询中的结果,因此只有结果>表中的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

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

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

输出:

Product Country  Count
0       A     USA      2
1       B   India      2

最新更新