I have a Pandas DataFrame df
that stores some numeric values :
print(df)
value
0 0
1 2
2 4
3 5
4 8
And I have a function that converts a numerical value to a one-hot vector
print(to_categorical(0))
[1 0 0 0 0 0 0 0 0 0]
print(to_categorical(5))
[0 0 0 0 0 5 0 0 0 0]
etc...
So, I can call my function over my columns of numeric value :
print(to_categorical(df['value'))
[[1 0 0 0 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 0 0 1 0]]
And now I want to store my results as a new column. Here is what I expect from my example :
df['one-hot'] = to_categorical(df['value')
print(df)
value one-hot
0 0 [1 0 0 0 0 0 0 0 0 0]
1 2 [0 0 1 0 0 0 0 0 0 0]
2 4 [0 0 0 0 1 0 0 0 0 0]
3 5 [0 0 0 0 0 1 0 0 0 0]
4 8 [0 0 0 0 0 0 0 0 1 0]
But this give me an error since pandas tries to flatten my array into multiple colums. How can I do that ?