python - Remove substring and merge rows in python/pandas
问题描述
my df:
description total average number
0 NFL football (white) L 49693 66 1007
1 NFL football (white) XL 79682 74 1198
2 NFL football (white) XS 84943 81 3792
3 NFL football (white) S 78371 73 3974
4 NFL football (blue) L 99482 92 3978
5 NFL football (blue) M 32192 51 3135
6 NFL football (blue XL 75343 71 2879
7 NFL football (red) XXL 84391 79 1192
8 NFL football (red) XS 34727 57 992
9 NFL football (red) L 44993 63 1562
What I would like to do is remove the sizes and be left with a sum total, mean average and sum number for each colour of football:
description total average number
0 NFL football (white) 292689 74 9971
1 NFL football (blue) 207017 71 9992
2 NFL football (red) 164111 66 3746
Any suggestions much appreciated!
解决方案
您可以groupby
重新格式化的description
字段(不修改 的原始内容description
),其中重新格式化是通过用空格分隔并使用.str.split()
,排除最后一部分来完成的.str.join()
。然后与 聚合.agg()
。
通过四舍五入和强制转换以与.round()
和整数进一步将输出重新格式化为所需的输出.astype()
。
(df.groupby(
df['description'].str.split(' ').str[:-1].str.join(' ')
)
.agg({'total': 'sum', 'average': 'mean', 'number': 'sum'})
.round(0)
.astype(int)
).reset_index()
结果:
description total average number
0 NFL football (blue) 207017 71 9992
1 NFL football (red) 164111 66 3746
2 NFL football (white) 292689 74 9971
推荐阅读
- c# - 如何在变量 C# 中捕获 IActionResult 方法返回的状态代码
- html - 移至样式表时未呈现 CSS
- marklogic - 如何在 Optic API 中使用 fn:contains()、fn:starts-with() 和 fn:ends-with
- node.js - 如何在NodeJS API中过滤mongoDB,检查值是否包含在数组中的对象中
- python - 如何在 conftest.py > Logger 方法中动态获取测试用例的名称
- c++ - 在类中声明变量时,'uint'在c ++中意味着什么?
- javascript - 在 MAPBOX GL JS 中将 lat 和 long 转换为地址名称
- javascript - Material UI - 在 TextField 中垂直显示装饰
- python - 如何在给定时间使用 Selenium 关闭浏览器选项卡
- javascript - 使 vue 小部件背景透明