Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一组一组,有熊猫_Python_Pandas_Group By - Fatal编程技术网

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'

我正试图在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().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)]