python - 如何正确旋转熊猫数据框
问题描述
我有以下数据框:
df = pd.DataFrame([{'answers': ['No'], 'questionId': 'applicationSubmitted'}, {'answers': ['NOT_READY_TO_ENROLL'], 'questionId': 'status'}, {'answers': ['Intro'], 'questionId': 'salesProcessReached'}, {'answers': [True], 'questionId': 'noTimeToCoverCurrentPlan'}, {'answers': [''], 'questionId': 'currentPlan'}, {'answers': ['No'], 'questionId': 'scheduleFollowUp'}])
应用数据透视函数后df.pivot(columns='questionId')
,我得到下表:
如何调整枢轴以使所有答案仅显示 1 行,标题仅显示 1 行而不是 2 行。
谢谢
解决方案
pivot_table
与aggfunc
as一起使用max
:
df.pivot_table(columns='questionId', aggfunc=max)
输出:
questionId applicationSubmitted currentPlan noTimeToCoverCurrentPlan salesProcessReached scheduleFollowUp status
answers [No] [] [True] [Intro] [No] [NOT_READY_TO_ENROLL]
推荐阅读
- html - 将网站部署到 Netlify 时不出现 FontAwesome 图标
- javascript - 在使用 jsconfig.conf 和 webpack 管理的 js 项目中导入图像
- c++ - 香农熵算法返回负值
- java - Firebase - 允许用户只访问自己的数据
- node.js - 渲染页面时 NextJS 错误,发布后
- uwp - 调试或发布我应该将哪些文件复制到 uwp 应用程序的 nuget 打包中的目标位置?
- arrays - 电子表格 IF 大于 0,使另一个单元格等于这个
- c# - 来自 AWS 的 API 调用失败
- python - 为什么 pandas 0.25.3 中的多索引排序这么慢
- web-scraping - 使用 puppeteer 优化网页抓取