1

Assuming we have a df as follows:

id A B 50 1 5 60 2 6 70 3 7 80 4 8 

I would like to know as to how can normalize just the column B, between 0 and 1, while keeping the other columns id and column A completely unaffected?

Edit 1: If I do the following

import pandas as pd df = pd.DataFrame({ 'id' : ['50', '60', '70', '80'], 'A' : ['1', '2', '3', '4'], 'B' : ['5', '6', '7', '8'] }) from sklearn import preprocessing min_max_scaler = preprocessing.MinMaxScaler() X_minmax = min_max_scaler.fit_transform(df.values[:,[2]]) 

I get the X_minmax as follows

0 0.333333 0.666667 1 

I want these 4 values to be placed in place of the column B in the dataframe df without changing the other 2 columns looking as below:

 id A B 50 1 0 60 2 0.333333 70 3 0.666667 80 4 1 
3

2 Answers 2

2

You can reassign the value of the column:

df.B = (df.B - df.B.mean()) / (df.B.max() - df.B.min()) 
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, @dzang. Thank you for the response. The values when I do this method range from -1 to 1. What can I do if I need the values between 0 and 1?
df.B = (df.B - df.B.min()) / (df.B.max() - df.B.min())
See Quang comment
1

You might want to do something like this.

import sklearn.preprocessing as preprocessing df=pd.DataFrame({'id':[50,60,70,80],'A':[1,2,3,4],'B':[5,6,7,8]}) float_array = df['B'].values.astype(float).reshape(-1,1) min_max_scaler = preprocessing.MinMaxScaler() scaled_array = min_max_scaler.fit_transform(float_array) df['B']=scaled_array 

1 Comment

Perfect. Was looking for something like this :) Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.