鉴于一系列看起来像:
0 foo
1 bar
2 foo
3 foo
4 bar
5 baz
如何创建一个数据框,其中每列是系列中唯一值的掩码?在这个例子中,它看起来像:
foo bar baz
0 True False False
1 False True False
2 True False False
3 True False False
4 False True False
5 False False True
解决方法:
使用get_dummies
s.str.get_dummies().astype(bool)
Out[392]:
bar baz foo
0 False False True
1 True False False
2 False False True
3 False False True
4 True False False
5 False True False
或者我们尝试一些新的交叉表
pd.crosstab(s.index,s).astype(bool)
Out[395]:
a bar baz foo
row_0
0 False False True
1 True False False
2 False False True
3 False False True
4 True False False
5 False True False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。