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 693
深忆病人
深忆病人 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:34

    I like crewbum's answer, probably this is faster (sorry, didn't tested this yet, but i avoid sorting everything):

    df.groupby('obj_id').agg(lambda df: df.values[df['data_date'].values.argmax()])
    

    it uses numpys "argmax" function to find the rowindex in which the maximum appears.

提交回复
热议问题