Replace single float values in pandas series with array

25 views Asked by At

I am trying to create a one-hot encoding for target values using the built-in iris dataset with pandas. I have split the data into features and target labels. The target labels are a pandas series (single column). I would like to replace the single values with an array so as to represent the target values as one-hot encoding.

I cannot seem to find a way to do this?

Please help! (https://i.stack.imgur.com/GMRuW.png)

I tried to use the .replace method to replace the floats with arrays

1

There are 1 answers

0
Grant On

Pandas has the get_dummies() function for doing this

import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
target = pd.Series(data.target)
pd.get_dummies(target)


    0       1       2
0   True    False   False
1   True    False   False
2   True    False   False
...