[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.