determine column maximum value per another column in pandas dataframe

前端 未结 1 602
长情又很酷
长情又很酷 2021-01-25 04:34

I have a dataframe contains location Id, store name and store revenue. I want to determine the store that has the maximum revenue per area

I wrote a code for that, but n

相关标签:
1条回答
  • 2021-01-25 04:44

    Create boolean mask by compare by eq (==) and convert it to integers - 0, 1 to False, True:

    s = dframe.groupby("Loc_Id")["Revenue"].transform('max')
    dframe["max_value"]= s.eq(dframe["Revenue"]).astype(int)
    print (dframe)
       Loc_Id Store  Revenue  max_value
    0       1     A       50          0
    1       2     B       70          0
    2       2     C       45          0
    3       1     B       35          0
    4       2     D       80          1
    5       1     B       70          1
    6       3     A       90          1
    7       3     C       65          0
    
    0 讨论(0)
提交回复
热议问题