Pandas: Difference to previous value

前端 未结 2 871
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 11:26

Given a Pandas Data Frame that looks like this

GROUP   VALUE    MASK
  1        5     false
  2       10     false
  2       20     false
  1        7     tr         


        
2条回答
  •  逝去的感伤
    2021-01-18 11:53

    use groupby, diff, 'MASK'

    pd.concat([df.VALUE, df.groupby('GROUP').VALUE.diff()],
              axis=1, keys=['VALUE', 'DIFF'])[df.MASK]
    

提交回复
热议问题