pandas :将列拆分为具有唯一值的多列 [英] Pandas: split column into multiple columns with unique values
本文介绍了 pandas :将列拆分为具有唯一值的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有以下数据框:
A
0 Me
1 Myself
2 and
3 Irene
4 Me, Myself, and Irene
需要转换为:
Me Myself and Irene
0 1 0 0 0
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 1 1 1 1
正在寻找任何建议.
推荐答案
您可以使用 get_dummies
与
You can use get_dummies
with reindex
by all possible categories:
df1 = pd.DataFrame({'A': ['Me', 'Myself', 'and', 'Irene']})
df2= pd.DataFrame({'A': ['Me', 'Myself', 'and']})
df3 = pd.DataFrame({'A': ['Me', 'Myself', 'or', 'Irene']})
all_categories = pd.concat([df1.A, df2.A, df3.A]).unique()
print (all_categories)
['Me' 'Myself' 'and' 'Irene' 'or']
df1 = pd.get_dummies(df1.A).reindex(columns=all_categories, fill_value=0)
print(df1)
Me Myself and Irene or
0 1 0 0 0 0
1 0 1 0 0 0
2 0 0 1 0 0
3 0 0 0 1 0
df2 = pd.get_dummies(df2.A).reindex(columns=all_categories, fill_value=0)
print(df2)
Me Myself and Irene or
0 1 0 0 0 0
1 0 1 0 0 0
2 0 0 1 0 0
df3 = pd.get_dummies(df3.A).reindex(columns=all_categories, fill_value=0)
print(df3)
Me Myself and Irene or
0 1 0 0 0 0
1 0 1 0 0 0
2 0 0 0 0 1
3 0 0 0 1 0
这篇关于 pandas :将列拆分为具有唯一值的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文