1

I have the following dataframe df;

 Name Date Attr1 Attr2 Attr3 0 Joe 26-12-2007 53.45 53.4500 52.7200 1 Joe 27-12-2007 52.38 52.7399 51.0200 2 Joe 28-12-2007 51.71 51.8500 50.7300 

I would like to scale the floating values in columns Attr1, Attr2, Attr3 to between 0 and 1. The highest value in a column is scaled to 1. Please note that not all the columns are to be scaled.

I am using Python 3.6.

The following code will scale all the columns but I need to scale selected columns. Another problem is that some columns are in date and string form. The code below will encounter problems converting the values to floating.

from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() scaled_values = scaler.fit_transform(df) df.loc[:,:] = scaled_values 
6
  • Have you tried writing code to solve this problem? Commented Aug 12, 2018 at 12:33
  • Take a look at sklearn.preprocessing.quantile_transform Commented Aug 12, 2018 at 12:35
  • It's not a duplicate. That question normalizes all the columns. I need to normalize selected columns. Commented Aug 12, 2018 at 12:37
  • Well you hadn't said that at the point I tagged as a duplicate. You've edited it in since. Commented Aug 12, 2018 at 12:37
  • And it only normalizes what you choose to pass to the function. Take a subset of the columns and use that. You're not forced to pass the whole df Commented Aug 12, 2018 at 12:39

1 Answer 1

1

Solution is:

In:

import pandas as pd data = pd.DataFrame({'Name':['John','Sara','Martin'],'first':[53.45, 55.51, 51.22],'second':[51.45, 54.51, 57.22],'third':[50.45, 54.51, 58.22]}) data 

Out:

 Name first second third 0 John 53.45 51.45 50.45 1 Sara 55.51 54.51 54.51 2 Martin 51.22 57.22 58.22 

In:

from sklearn.preprocessing import MinMaxScaler sclr = MinMaxScaler() new_df = sclr.fit_transform(data[['first', 'second', 'third']]) 

Out:

array([[ 0.51981352, 0. , 0. ], [ 1. , 0.53032929, 0.52252252], [ 0. , 1. , 1. ]]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.