I have a column in my DataFrame as shown below:
City
1) London
2) Birmingham
3) Lahore
4) Paris
.
.
I want to convert this column into one hot encoded. The output should look like as:
City
1) [1,0,0,0]
2) [0,1,0,0]
3) [0,0,1,0]
4) [0,0,0,1]
code showing output:
pd.get_dummies(dff['city (S)'], prefix='city_')
city__Kingston city__Liverpool city__London
0 0 0
0 0 0
0 0 1
0 0 0
I want in a single column only, not separate
Hard to be sure without actually looking at the data but something like this should do the trick:
import pandas as pd
pd.get_dummies(df['City'], prefix='city_')
You could use the OneHotEncoder from sklearn
from sklearn.preprocessing import OneHotEncoder
oe = OneHotEncoder(sparse=False)
df['City'] = oe.fit_transform(df['City'])
sparse=False shows all the 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.