Python 一组一组,有熊猫
我正试图在Python中实现上述功能。我尝试了不同组合的Python 一组一组,有熊猫,python,pandas,group-by,Python,Pandas,Group By,我正试图在Python中实现上述功能。我尝试了不同组合的过滤器,nunique,agg,但没有任何效果。有什么建议吗 例: df 所以我希望结果是: df id airport 1 lax 1 ohare 2 phl 3 lax 2 mdw 2 lax 2 sfw 2 tpe 您可以与或一起使用: 或: 使用groupby和count: df1 = df.groupby('id')['airport'
过滤器
,nunique
,agg
,但没有任何效果。有什么建议吗
例:
df
所以我希望结果是:
df
id airport
1 lax
1 ohare
2 phl
3 lax
2 mdw
2 lax
2 sfw
2 tpe
您可以与或一起使用:
或:
使用groupby和count:
df1 = df.groupby('id')['airport'].nunique().reset_index().query('airport > 3')
print (df1)
id airport
1 2 5
过滤器:
df_new = df.groupby('id').count()
s = df.groupby('id')['airport'].nunique()
print (s)
id
1 2
2 5
3 1
Name: airport, dtype: int64
df1 = s[s > 3].reset_index()
print (df1)
id airport
0 2 5
df1 = df.groupby('id')['airport'].nunique().reset_index().query('airport > 3')
print (df1)
id airport
1 2 5
df_new = df.groupby('id').count()
df_new = df_new[(df_new['airport'] > 3)]