This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing one hot encoder | |
from sklearn from sklearn.preprocessing import OneHotEncoder | |
# creating one hot encoder object | |
onehotencoder = OneHotEncoder() | |
#reshape the 1-D country array to 2-D as fit_transform expects 2-D and finally fit the object | |
X = onehotencoder.fit_transform(data.Country.values.reshape(-1,1)).toarray() | |
#To add this back into the original dataframe | |
dfOneHot = pd.DataFrame(X, columns = ["Country_"+str(int(i)) for i in range(data.shape[1])]) | |
df = pd.concat([data, dfOneHot], axis=1) | |
#droping the country column | |
df= df.drop(['Country'], axis=1) | |
#printing to verify | |
print(df.head()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment