Datalearner
Datalearner

Reputation: 185

Scale columns in dataframe based on another column

I have the dataframe shown below :

column1 column2 column3
1 0.01 0.001
2 0.02 0.002
3 0.03 0.003

I want to scale column 2 and column 3 based on the min and max scaler of column 1, in order to obtain the following :

column1 column2 column3
1 1 1
2 2 2
3 3 3

These numbers are just imputed to understand the question easily.

All the solutions I have seen online basically uses a min max where they scale the columns between 0-1 but I want to scale it based on the min max of the first column.

Upvotes: 0

Views: 712

Answers (3)

zohre Anisi
zohre Anisi

Reputation: 1

You can do this:

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler() 
  
df[["column_i"]] = scaler.fit_transform(df[["column_i"]])

Upvotes: 0

mozway
mozway

Reputation: 260455

@Adrien's answer is very nice, but if you want to do it without external dependency:

MIN = df.min()
MAX = df.max()
(df-MIN)/(MAX-MIN)*(MAX['column1']-MIN['column1'])+MIN['column1']

Upvotes: 1

Adrien
Adrien

Reputation: 433

You can fit a min-max scaler on column 1 and apply it to other columns :

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()
scaler.fit(df['column1'])

Apply it to column i :

df['column_i'] = scaler.transform(df['column_i'])

Upvotes: 1

Related Questions