Normalizing Pandas Series With Condition
I'm learning Python/Pandas with a DataFrame having the following structure: import pandas as pd df = pd.DataFrame({'key' : [111, 222, 333, 444, 555, 666, 777, 888, 999],
Solution 1:
Idea is convert -1
values to missing values:
cols = ['score1','score2']
df[cols] = df[cols].mask(df[cols] == -1)
x = df[cols].values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = df.join(pd.DataFrame(x_scaled, columns=cols).add_prefix('norm_'))
print (df)
key score1 score2 norm_score1 norm_score2
0 111 NaN 2.0 NaN 0.2
1 222 0.0 2.0 0.000000 0.2
2 333 2.0 NaN 0.133333 NaN
3 444 NaN 10.0 NaN 1.0
4 555 7.0 0.0 0.466667 0.0
5 666 0.0 5.0 0.000000 0.5
6 777 15.0 NaN 1.000000 NaN
7 888 0.0 1.0 0.000000 0.1
8 999 1.0 0.0 0.066667 0.0
Post a Comment for "Normalizing Pandas Series With Condition"