marqram
marqram

Reputation: 789

Min and max row from pandas groupby

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

gb = df.groupby('groupID')

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.

How can I calculate this?

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

Upvotes: 2

Views: 2177

Answers (1)

jezrael
jezrael

Reputation: 863741

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()]

Upvotes: 2

Related Questions