簡體   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