Since I don't know the name of the concept I am looking for, googling it has made but no success...
I need to convert something like:
a b c d
1 2 'Hey' 4
2 2 'Hello' 4
1 2 'World' 3
to something like:
a==1 a==2 b==2 c=='Hey' c=='Hello' c=='World' d==3 d==4
1 0 1 1 0 0 0 1
0 1 1 0 1 0 0 1
1 0 1 0 0 1 1 0
I am using python 3.8 and pandas 1.1.3 Many thanks in advance...
Use get_dummies
with convert all values to strings and set prefix_sep
parameter:
df = pd.get_dummies(df.astype(str), prefix_sep='==')
print (df)
a==1 a==2 b==2 c=='Hello' c=='Hey' c=='World' d==3 d==4
0 1 0 1 0 1 0 0 1
1 0 1 1 1 0 0 0 1
2 1 0 1 0 0 1 1 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.