2

I have the following example of code

import pandas as pd

df = pd.DataFrame({'a':[1, 2, 3],
                   'b':[10, 20, 30],
                   'c':[100, 200, 300],
                   'd':['q', 'w', 'r']})

Get the values of the dataframe

In [12]: df[['a', 'b', 'c']].values
Out[12]: 
array([[  1,  10, 100],
       [  2,  20, 200],
       [  3,  30, 300]])

Then i normalize the values

from sklearn.preprocessing import normalize

norm = normalize(df[['a', 'b', 'c']].values, axis=0)

In [11]: norm
Out[11]: 
array([[0.26726124, 0.26726124, 0.26726124],
       [0.53452248, 0.53452248, 0.53452248],
       [0.80178373, 0.80178373, 0.80178373]])

Now i want to do something like

df[['a', 'b', 'c']].values = norm

But i get the error (i knew about it)

AttributeError: can't set attribute

How can i modify those values without affecting the other parts of the dataframe (i.e the column 'e' and the indices); just the values.

Thanks.

moctarjallo
  • 1,479
  • 1
  • 16
  • 33

1 Answers1

3

You do not need call value

df[['a', 'b', 'c']]=norm
df
Out[342]: 
          a         b         c  d
0  0.267261  0.267261  0.267261  q
1  0.534522  0.534522  0.534522  w
2  0.801784  0.801784  0.801784  r
BENY
  • 317,841
  • 20
  • 164
  • 234