Pandas Groupby的最小和最大行 [英] Min and max row from pandas groupby

查看:108
本文介绍了Pandas Groupby的最小和最大行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,已将其分组在"groupID"列上.

I have a pandas dataframe which I grouped on column 'groupID'.

gb = df.groupby('groupID')

每行是a,具有x和y坐标以及一个残差(与x = y线的距离).现在,我想在残差最大和最小的组中找到2个点(x,y)之间的梯度.我知道如何使用gb ['residual'].min()和max(),但这没有给我那一行.

Each row is a has a x and y coordinate and a residual (distance from the line x=y). Now I want to find the gradient between 2 points(x,y) in a group where the residual is the biggest and the smallest. I know how to use gb['residual'].min() and max() but that does not give me the row.

我该如何计算?

df[gb ['residual'].idxmin()]

推荐答案

我认为您需要通过

I think you need find all indices from max and min value of column residual by DataFrameGroupBy.idxmax and DataFrameGroupBy.idxmin and then select by loc:

df1 = df.loc[df.groupby('groupID').residual.idxmax()]
df2 = df.loc[df.groupby('groupID').residual.idxmin()]

这篇关于Pandas Groupby的最小和最大行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆