Soren Christensen
Soren Christensen

Reputation: 376

How to use the max_min scaler in Python

I have a pandas dataframe:

from sklearn.preprocessing import MinMaxScaler

data = pd.DataFrame({'x': [1,2,3,4,5],
              'name': ['jo', 'ellen','jo', 'ellen' ,'jo' ]} )


min_max_scaler = MinMaxScaler()
data['scaled_x'] = dataframe.groupby('name')['x'].transform(lambda x: min_max_scaler.fit_transform(x))

I cant get it to work.. how do I scale by name?

Upvotes: 0

Views: 338

Answers (1)

mozway
mozway

Reputation: 260455

Many sklearn functions expect a 2D input, even if only 1D data. You need to convert your Series to DataFrame with to_frame. Then you need to convert the output back to 1D using ravel:

data['scaled_x'] = data.groupby('name')['x'].transform(lambda c: min_max_scaler.fit_transform(c.to_frame()).ravel())

output:

   x   name  scaled_x
0  1     jo       0.0
1  2  ellen       0.0
2  3     jo       0.5
3  4  ellen       1.0
4  5     jo       1.0

Upvotes: 1

Related Questions