Merging a pandas groupby result back into DataFrame

后端 未结 2 1843
迷失自我
迷失自我 2021-02-19 23:41

I have a DataFrame that looks like this...

   idn value  
0  ID1    25
1  ID1    30
2  ID2    30
3  ID2    50

I want to add another column to t

2条回答
  •  走了就别回头了
    2021-02-20 00:10

    Use the transform method on a groupby object:

    In [5]: df['maxval'] = df.groupby(by=['idn']).transform('max')
    
    In [6]: df
    Out[6]: 
       idn  value  maxval
    0  ID1     25      30
    1  ID1     30      30
    2  ID2     30      50
    3  ID2     50      50
    

提交回复
热议问题