0

I want to rescale my pandas dataframe using sklearn's MinMaxScaler function, like in this tutorial.

The data I have is in mydata,

            x1   x2   x3  x4    x5  
Date                                                                 
2015-03-01  90  180  113  12  2125   
2015-03-02  64  180  107   5  2121   
2015-03-03  79  170  110  12  2009   
2015-03-04  82  160  107   6  2020   
2015-03-05  69  152  108  13  1979   
2015-03-06  51  229   95  10  2120

and my code to only rescale columns x1, x2, x3 is

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler, StandardScaler

### load mydata

scaler = MinMaxScaler()

mydata_scaled = scaler.fit_transform(mydata.values)
mydata_scaled = pd.DataFrame(mydata_scaled, columns=['x1','x2','x3'])

mydata_scaled.head(5)

but I get an error ValueError: Shape of passed values is (5, 6), indices imply (3, 6). There is another solution using Column_Transformer, but I want to know why the MinMaxScaler method doesn't work.

Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75

1 Answers1

1

I found a solution:

mydata[['x1','x2','x3']] = MinMaxScaler().fit_transform(mydata[['x1','x2','x3']])

It's similar to the solution in Standardize some columns in Python Pandas dataframe?

Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75