如何将 pandas 数据框转换为具有 Column==value 列的 table?

How to convert pandas dataframe to a table with Column==value columns?

因为我不知道我正在寻找的概念的名称,谷歌搜索了它但没有成功...

我需要转换如下内容:

   a   b        c   d
   1   2    'Hey'   4
   2   2  'Hello'   4
   1   2  'World'   3

类似于:

   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

我正在使用 python 3.8 和 pandas 1.1.3 非常感谢...

使用 get_dummies 将所有值转换为字符串并设置 prefix_sep 参数:

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