mat*_*hew 3 python numpy pandas
我想保留一个具有每个id最大系列的记录.所以对于每个id,我需要一行.我想我需要类似的东西
df_new = df.groupby('id')['series'].nlargest(1)
Run Code Online (Sandbox Code Playgroud)
,但那肯定是错的.
这就是我的数据集的外观:
id series s1 s2 s3
1 2 4 9 1
1 8 6 2 2
1 3 9 1 3
2 9 4 1 5
2 2 2 5 5
2 5 1 7 8
3 6 7 2 3
3 2 4 4 1
3 1 3 9 9
Run Code Online (Sandbox Code Playgroud)
这应该是结果:
id series s1 s2 s3
1 8 6 2 2
2 9 4 1 5
3 6 7 2 3
Run Code Online (Sandbox Code Playgroud)
您希望groupby在"id"列上获取IIUC,并获取"Series"值最大的索引标签,idxmax()并使用它在orig df中索引:
In [91]:
df.loc[df.groupby('id')['series'].idxmax()]
Out[91]:
id series s1 s2 s3
1 1 8 6 2 2
3 2 9 4 1 5
6 3 6 7 2 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |