pandas - get most recent value of a particular column indexed by another column (get maximum value of a particular column indexed by another column)

前端 未结 6 678
深忆病人
深忆病人 2020-12-01 11:05

I have the following dataframe:

   obj_id   data_date   value
0  4        2011-11-01  59500    
1  2        2011-10-01  35200 
2  4        2010-07-31  24860          


        
6条回答
  •  隐瞒了意图╮
    2020-12-01 11:43

    The aggregate() method on groupby objects can be used to create a new DataFrame from a groupby object in a single step. (I'm not aware of a cleaner way to extract the first/last row of a DataFrame though.)

    In [12]: df.groupby('obj_id').agg(lambda df: df.sort('data_date')[-1:].values[0])
    Out[12]: 
             data_date  value
    obj_id                   
    1       2009-07-28  15860
    2       2011-10-01  35200
    4       2011-11-01  59500
    

    You can also perform aggregation on individual columns, in which case the aggregate function works on a Series object.

    In [25]: df.groupby('obj_id')['value'].agg({'diff': lambda s: s.max() - s.min()})
    Out[25]: 
              diff
    obj_id        
    1            0
    2       165000
    4        34640
    

提交回复
热议问题