Pandas: Difference between largest and smallest value within group

后端 未结 3 1723
心在旅途
心在旅途 2020-11-30 02:45

Given a data frame that looks like this

GROUP VALUE
  1     5
  2     2
  1     10
  2     20
  1     7

I would like to compute the differe

3条回答
  •  臣服心动
    2020-11-30 02:57

    You can use groupby(), min(), and max():

    df.groupby('GROUP')['VALUE'].apply(lambda g: g.max() - g.min())
    

提交回复
热议问题