pandas - groupby and re-scale values

前端 未结 1 1702
囚心锁ツ
囚心锁ツ 2021-01-20 01:12

I would like to add a rescaled column to this dataframe:

I,Value
A,1
A,4
A,2
A,5
B,1
B,2
B,1

so that the new column (let\'s call it s

相关标签:
1条回答
  • 2021-01-20 01:40

    If you added the 'Value' column to your code then it would work:

    In [69]:
    df.groupby('I')['Value'].apply(lambda x: (x-min(x))/(max(x)-min(x)))
    
    Out[69]:
    0    0.00
    1    0.75
    2    0.25
    3    1.00
    4    0.00
    5    1.00
    6    0.00
    dtype: float64
    

    The pandas method version is the following which produces the same result:

    In [67]:
    df['Normalised'] = df.groupby('I')['Value'].apply(lambda x: (x-x.min())/(x.max()-x.min()))
    df
    
    Out[67]:
       I  Value  Normalised
    0  A      1        0.00
    1  A      4        0.75
    2  A      2        0.25
    3  A      5        1.00
    4  B      1        0.00
    5  B      2        1.00
    6  B      1        0.00
    
    0 讨论(0)
提交回复
热议问题