我有一个这样的数据框:
data = np.array([["userA","event2, event3"],
['userB',"event3, event4"],
['userC',"event2"]])
data = pd.DataFrame(data)
0 1
0 userA "event2, event3"
1 userB "event3, event4"
2 userC "event2"
现在我想得到一个像这样的数据框:
0 event2 event3 event4
0 userA 1 1
1 userB 1 1
2 userC 1
有人可以帮忙吗?
最佳答案
看来您需要用 get_dummies
替换0
来清空string
s:
df = data[[0]].join(data[1].str.get_dummies(', ').replace(0, ''))
print (df)
0 event2 event3 event4
0 userA 1 1
1 userB 1 1
2 userC 1
详细信息:
print (data[1].str.get_dummies(', '))
event2 event3 event4
0 1 1 0
1 0 1 1
2 1 0 0
关于python Pandas : split comma-separated column into new columns - one per value,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48823152/