熊猫枢轴多列,索引和值
我有以下数据集
结果_by关注 | _up_result | colloct_up_number | # | % | |
---|---|---|---|---|---|
0 | 用户1件 | 好 | 1 | 30 | 30 |
1 | 用户2件 | 好 | 2 | 65 | 65 |
2 | 用户3 | 糟糕 | 3 | 5 | 0.05 |
我想枢纽:
后续结果
和>
>
作为索引
- 跟踪编号作为列
#
和%
作为值
pivot = df.head(3).pivot(columns=['follow_up_number'], values=["#", '%'], index=['follow_up_result', 'resulted_by'])
我希望后续号码超过值,这就是我的实现方式:
pivot = df.head(3).pivot(columns=['follow_up_result', 'resulted_by'], values=["#", '%'], index=['follow_up_number'])
pivot = pivot.stack(level=0).T
注意如何切换列
和indexes
。
我希望列名与值相同。
有办法做到吗?
有没有更好的方法来实现我所需的目标,而无需在列之间切换
和indexes
?
代码snippet:
I have the following dataset
resulted_by | follow_up_result | follow_up_number | # | % | |
---|---|---|---|---|---|
0 | User 1 | good | 1 | 30 | 30 |
1 | User 2 | good | 2 | 65 | 65 |
2 | User 3 | bad | 3 | 5 | 0.05 |
I want to Pivot:
follow up result
andresulted by
as indexesfollow up number
as a column#
and%
as values
pivot = df.head(3).pivot(columns=['follow_up_number'], values=["#", '%'], index=['follow_up_result', 'resulted_by'])
However, I want the follow up number to be above the values, here is how I achieved that:
pivot = df.head(3).pivot(columns=['follow_up_result', 'resulted_by'], values=["#", '%'], index=['follow_up_number'])
pivot = pivot.stack(level=0).T
Notice how I switch columns
and indexes
.
I want the column names to be at the same level as the values.
Is there a way to do that?
Is there a better way to achieve what I need without switching between columns
and indexes
?
Code Snippet:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论