熊猫枢轴多列,索引和值

发布于 2025-02-02 19:57:57 字数 2186 浏览 1 评论 0原文

我有以下数据集

结果_by关注_up_resultcolloct_up_number
0用户1件13030
1用户2件26565
2用户3糟糕350.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:

https://onecompiler.com/pypython/3y5gzm7hu

I have the following dataset

resulted_byfollow_up_resultfollow_up_number#%
0User 1good13030
1User 2good26565
2User 3bad350.05

I want to Pivot:

  • follow up result and resulted by as indexes
  • follow up number as a column
  • # and % as values
pivot = df.head(3).pivot(columns=['follow_up_number'], values=["#", '%'], index=['follow_up_result', 'resulted_by'])

enter image description here

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.

enter image description here

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:

https://onecompiler.com/python/3y5gzm7hu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文