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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

Hey now i want to replace column d with something like
[1, 0, 0], [0, 1, 0], [0, 0, 1] for every category q, w and r. How can I ? Thanks.
@mctrjallohdf.d=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
because i want to make it ready to train a keras model classifier
I want to compute those binary vectors in the first place
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.