旋转具有重复值的数据框
我有一个类似的数据框:
df = pd.DataFrame({'id':{1,2,3,4,4},
'course':{'english', 'art', 'math', 'english', 'chem'},
'result':{'a','b','c','a','b'}})
我想旋转此功能:
course English art math sci chem
id
1 a - - - -
2 - b - - -
3 - - c - -
4 a - - - b
注意:ID,resuct&课程。 由于重复的值,我无法透视它。
I have a dataframe like this:
df = pd.DataFrame({'id':{1,2,3,4,4},
'course':{'english', 'art', 'math', 'english', 'chem'},
'result':{'a','b','c','a','b'}})
I want to pivot this such that:
course English art math sci chem
id
1 a - - - -
2 - b - - -
3 - - c - -
4 a - - - b
Note: There are repeated values in ID, results & Courses.
Due to the duplicate values, I am not able to pivot it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pivot
正常工作。我认为问题是您的意见。您为每个键都有一组,因此重复项消失了。使用列表并有效。pivot
works just fine. I think the issue is with your input. You have a set for each key, so the duplicates disappear. Use a list and it works.让我们使用
set_index
和unstack
:输出:
Let's use
set_index
andunstack
:Output: