Pandas groupby in combination with sklearn preprocessing

前端 未结 1 715
無奈伤痛
無奈伤痛 2021-02-20 17:37

I want to group my DataFrame by specific column and then apply a sklearn preprocessing MinMaxScaler and store the scaler object.

My at the moment starting point:

1条回答
  •  日久生厌
    2021-02-20 18:13

    you can do it in one direction:

    In [62]: from sklearn.preprocessing import minmax_scale
    
    In [63]: df
    Out[63]:
      ID   VL  SC
    0  A    0   0
    1  A   10   1
    2  A   10   1
    3  B  100   0
    4  B  100   0
    5  B  200   1
    
    In [64]: df['SC'] = df.groupby('ID').VL.transform(lambda x: minmax_scale(x.astype(float)))
    
    In [65]: df
    Out[65]:
      ID   VL  SC
    0  A    0   0
    1  A   10   1
    2  A   10   1
    3  B  100   0
    4  B  100   0
    5  B  200   1
    

    but you will not be anle to use inverse_transform as each call of MinMaxScaler (for each group or each ID) will overwrite the information about your orginal features...

    0 讨论(0)
提交回复
热议问题