2

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()]
marqram
  • 725
  • 12
  • 26

1 Answers1

2

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()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks, that did the job. Didn't know about `loc` – marqram Dec 06 '16 at 09:45
  • 1
    @SriK - Problem is your `observation_timestamp` column have no datetimes, need `df.observation_timestamp = pd.to_datetime(observation_timestamp)` first – jezrael Jun 10 '18 at 14:10