Below implementation should work well. Note that the input of onehotencoder fit_transform
must not be 1-rank array and also output is sparse and we have used to_array()
to expand it.
import pandas as pdimport numpy as npfrom sklearn.preprocessing import LabelEncoderfrom sklearn.preprocessing import OneHotEncoderdata= [["AUS", "Sri"],["USA","Vignesh"],["IND", "Pechi"],["USA","Raj"]]df = pd.DataFrame(data, columns=['Country', 'Name'])X = df.valuesle = LabelEncoder()X_num = le.fit_transform(X[:,0]).reshape(-1,1)ohe = OneHotEncoder()X_num = ohe.fit_transform(X_num)print (X_num.toarray())X[:,0] = X_numprint (X)