Pandas Group By 和 Get Dummies [英] Pandas Group By And Get Dummies
本文介绍了Pandas Group By 和 Get Dummies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取每个唯一值的虚拟变量.想法是将数据框变成多标签目标.我该怎么做?
I want to make get dummy variables per unique value. Idea is to turn the data frame into a multi-label target. How can I do it?
数据:
ID L2
A Firewall
A Security
B Communications
C Business
C Switches
期望输出:
ID Firewall Security Communications Business Switches
A 1 1 0 0 0
B 0 0 1 0 0
C 0 0 0 1 1
我尝试过 pd.pivot_table
但它需要一个列来聚合.我也试过在这个链接上回答,但它总结了值,而不仅仅是变成二进制虚拟列.我将非常感谢您的帮助.非常感谢!
I have tried pd.pivot_table
but it requires a column to aggregate on. I have also tried answer on this link but it sums the values rather than just turning into binary dummy columns. I would much appreciate your help. Thanks a lot!
推荐答案
让我们set_index
然后get_dummies
,因为我们每个ID都有多个重复,我们需要sum
with level = 0
Let us set_index
then get_dummies
, since we have multiple duplicate in each ID ,we need to sum
with level = 0
s = df.set_index('ID')['L2'].str.get_dummies().max(level=0).reset_index()
Out[175]:
ID Business Communications Firewall Security Switches
0 A 0 0 1 1 0
1 B 0 1 0 0 0
2 C 1 0 0 0 1
这篇关于Pandas Group By 和 Get Dummies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文