如何获得没有前缀的假人?

qwe*_*rty 3 pandas

我想从两个不同的列中获取假人,但没有任何前缀。

数据说明:

   X    Y
 123  AAA
 456  BBB
 123  AAA
 789  CCC
Run Code Online (Sandbox Code Playgroud)

预期结果:

   X    Y  789  456  123  CCC  BBB  AAA
 123  AAA    0    0    1    0    0    1
 456  BBB    0    1    0    0    1    0
 123  AAA    0    0    1    0    0    1
 789  CCC    1    0    0    1    0    0
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 6

使用get_dummies智慧prefix=''prefix_sep=''参数,如果可能的话,一些数字列也将它们转换为字符串:

df = df.join(pd.get_dummies(df.astype(str), prefix='', prefix_sep=''))
print (df)
     X    Y  123  456  789  AAA  BBB  CCC
0  123  AAA    1    0    0    1    0    0
1  456  BBB    0    1    0    0    1    0
2  123  AAA    1    0    0    1    0    0
3  789  CCC    0    0    1    0    0    1
Run Code Online (Sandbox Code Playgroud)