python - 如何使用 value_counts 为一列与另一列派生新列
问题描述
我有包含许多列的数据框。
df2
TargetDescription Output_media_duration
0 VMN 4.0 16x9 25 - 1920x1080, 1280x720, 960x540... NaN
1 VMN 4.0 16x9 25 - 1920x1080, 1280x720, 960x540... NaN
2 XDCAM HD NTSC 1920x1080 MXF 8CA 661.120000
3 VMN 4.0 16x9 29.97 - 1920x1080, 1280x720, 960x... 285.647686
4 VMN 4.0 16x9 29.97 - 1920x1080, 1280x720, 960x... 402.697303
5 VMN 4.0 16x9 29.97 - 1920x1080, 1280x720, 960x... 269.597070
6 VMN 4.0 16x9 29.97 - 1920x1080, 1280x720, 960x... 307.059607
7 Caption QC HD MOV 2CA 2516.096917
8 QT Proxy 640x360 2997 12CA NaN
9 XDCAM HD NTSC 1920x1080 MXF 8CA 1414.785215
10 Caption QC HD MOV 2CA 1295.027067
11 QT Proxy 640x360 2398 4CA 2524.980792
12 Caption QC HD MOV 2CA 120.820700
13 Caption QC HD MOV 2CA 2516.096917
现在我想得到一个新的数据框,它会像这样向我展示
TargetDescription format_duration
1 VMN 4.0 16x9 25 - 1920x1080, 1280x720, 960x540... NaN
2 XDCAM HD NTSC 1920x1080 MXF 8CA 661.120000
3 VMN 4.0 16x9 29.97 - 1920x1080, 1280x720, 960x... 1656.561906
4 Caption QC HD MOV 2CA 2516.096917
5 QT Proxy 640x360 2997 12CA NaN
6 Caption QC HD MOV 2CA 2636.917
我如何在熊猫中实现这一点,在此先感谢
解决方案
df.groupby('TargetDescription')['Output_media_duration'].sum().reset_index(name ='format_duration')
推荐阅读
- alfresco - 在 Alfresco ADF 中链接 sidenav 和树视图组件
- c - 我从 C 中的字符串中删除 \n 但结果不是我需要的
- .htaccess - 我可以获得 301 重定向以显示来自目标页面的信息吗?
- r - 使用 R 闪亮的本地数据库存储:read.table 中的错误:列多于列名
- javascript - Javascript按数字和字母对字符串数组进行排序
- java - Keycloak 无法连接到在 Windows 上运行的 SQL Server
- c++ - 尝试显示字符数组C ++时出现奇怪的输出
- timeout - 当文本在会话中换行时出现问题
- python - 如何在特定数据子集上前滚并修改原始数据集?
- google-apps-script - Apps 脚本中 HTML 评估的目的是什么?