Python 数据帧二进制编码
我有一个如下所示的数据框:
User | Product |
---|---|
1 | a |
1 | b |
2 | a |
2 | c |
3 | b |
我想要每个用户 1 行,其中产品作为列,如果用户是否购买了该产品,它会给出 1 或 0,如何才能我做这个?
I have a dataframe that looks like this:
User | Product |
---|---|
1 | a |
1 | b |
2 | a |
2 | c |
3 | b |
I want 1 row per user with the products as columns where it gives a 1 or 0 if the user purchased the product or not, how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的是“交叉制表”或只是<交叉表。 Pandas 有
pd.crosstab
对于同样的。What you are looking for is "cross tabulation" or simply crosstab. Pandas has
pd.crosstab
for the same.