[英]pandas filter group by
I would like to filter results in this query, thus having only results >1 in table, if possible in a single line of code.我想过滤此查询中的结果,因此如果可能在一行代码中,表中只有结果 >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
you can filter the count column fe like this.您可以像这样过滤计数列 fe。 You can also change this to a single line like:您还可以将其更改为单行,例如:
table=df.groupby(['Product','Country'])['Age'].count().reset_index(name='count')[table["count"]>1]
Let's chain query
method:让我们链式query
方法:
table = (df.groupby(['Product','Country'])['Age'].count()
.reset_index(name='Count')
.query('Count > 1'))
table
Output: Output:
Product Country Count
0 A USA 2
1 B India 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.