python-3.x - 在python中,标题不在同一行
问题描述
我从一个较大的数据框(recent_grads)中提取了三列,如下所示......
df = recent_grads.groupby('Major_category')['Men', 'Women'].sum()
但是,当我打印 df 时,它出现如下...
Men Women
Major_category
Agriculture & Natural Resources 40357.0 35263.0
Arts 134390.0 222740.0
Biology & Life Science 184919.0 268943.0
Business 667852.0 634524.0
Communications & Journalism 131921.0 260680.0
Computers & Mathematics 208725.0 90283.0
Education 103526.0 455603.0
Engineering 408307.0 129276.0
Health 75517.0 387713.0
Humanities & Liberal Arts 272846.0 440622.0
Industrial Arts & Consumer Services 103781.0 126011.0
Interdisciplinary 2817.0 9479.0
Law & Public Policy 91129.0 87978.0
Physical Sciences 95390.0 90089.0
Psychology & Social Work 98115.0 382892.0
Social Science 256834.0 273132.0
如何将 Major_category 标题与 Men 和 Women 标题放在同一行?我试图将三列放在一个新的数据框中,如下所示......
df1 = df[['Major_category', 'Men', 'Women']].copy()
这给了我一个错误(Major_category 不在索引中)
解决方案
似乎您想将 groupby 对象转换回数据框尝试:
df['Major_category'].apply(pd.DataFrame)
推荐阅读
- rest - Blazor Webassembly 项目调用 REST API
- angular - Angular:为什么在“if 语句”中可以使用 const 作为布尔值?
- visual-studio-2019 - Visual Studio 2019 Release 文件夹中的哪些文件应该上传到 GitHub?
- c - 在没有 btsl 指令的情况下使用 inline asm 设置位
- node.js - 如何替换在 docker 容器内运行的 react js 应用程序的品牌资源(徽标、背景图像)
- delphi - 对控件进行分组的非可视组件,以便我可以使用对齐?
- c - 如何教授 Visual Studio Code 关于 __fp16?
- traveling-salesman - 模拟退火 + 2opt 并不比 2opt 好
- python - 将字典列表转换为熊猫中的行
- javascript - 是否可以在嵌入的 Tradingview 小部件上绘制模式(XABCD)?