Python pandas idxmax for multiple indexes in a dataframe

后端 未结 3 912
小蘑菇
小蘑菇 2021-02-10 00:52

I have a series that looks like this:

            delivery
2007-04-26  706           23
2007-04-27  705           10
            706         1089
            708         


        
3条回答
  •  梦谈多话
    2021-02-10 01:20

    If you have the following dataframe (you can always reset the index if needed with : df = df.reset_index() :

      Date  Del_Count  Del_Nb
    0  1/1      14      19   <
    1           11      17
    2  2/2      25      29   <
    3           21      27
    4           22      28
    5  3/3      34      36
    6           37      37
    7           31      39   <
    

    To find the max per Date and extract the relevant Del_Count you can use:

    df = df.ix[df.groupby(['Date'], sort=False)['Del_Nb'].idxmax()][['Date','Del_Count','Del_Nb']]
    

    Which would yeild:

     Date  Del_Count  Del_Nb
    0  1/1         14      19
    2  2/2         25      29
    7  3/3         31      39
    

提交回复
热议问题