how to make one hot encoding to column in data frame in python

I have dataset that includes categorial column for education level the initial values was 0,nan, high school, graduate school, university I have cleaned the data and convert it to the following values

0-> others 1-> high school 2-> graduate school 3-> university

in the same column, now I want to hot encode this column to 4 columns

I have tried to use scikit learn as following

onehot_encoder = OneHotEncoder()
onehot_encoded = onehot_encoder.fit_transform(df_csv['EDUCATION'])
print(onehot_encoded)

and I got this error

ValueError: Expected 2D array, got 1D array instead:
array=[3 3 3 ... 3 1 3].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.


Read more here: https://stackoverflow.com/questions/64938963/how-to-make-one-hot-encoding-to-column-in-data-frame-in-python

Content Attribution

This content was originally published by Amira Elsayed Ismail at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: