python - Pandas,Python:如何将行值转换为列并将另一列的值聚合为总和
问题描述
我正在尝试分析一个 covid 数据集,并且对如何通过 pandas 修复数据不知所措。数据集如下所示:
我试图让它看起来像这样:
April 2 | April 3 | April 4
unique_tests total unique tests for april 2 | total unique tests for april 3|total unique tests for april 4
positive total positive for april 2 | total positive for april 3 |total positive for april 4
negative total negative for april 2 | total negative for april 3 |total negative for april 4
remaining total remaining for april 2 | total remaining for april 3 |total remaining for april 4
我的日期截止到 4 月 24 日。
关于我如何实现这一点的任何想法?我无法使其与熊猫中的数据透视表一起使用
解决方案
利用:
#convert columns to numeric and date to datetimes
df = pd.read_csv(file, thousands=',', parse_dates=['date'])
#create custom format of datetimes and aggregate sum, last transpose
df1 = df.groupby(df['date'].dt.strftime('%d-%b')).sum().T
或者是否可以重新分配date
由新格式的日期时间填充的列:
df1 = df.assign(date = df['date'].dt.strftime('%d-%b')).groupby('date').sum().T
推荐阅读
- php - 以编程方式创建的 WooCommerce 订单 - 根据电子邮件而不是用户 ID 设置客户
- java - java中重定向控制台输入的问题
- python - 如何在熊猫中将multiindex更改为正常
- c# - C# 仅在存在时使用数组
- javascript - Autodesk Forge Viewer 调用内置扩展功能
- maven - “输入更改需要完全重建增量任务':compileKotlin'。” Gradle 构建时
- gradle - 使用包装器时如何设置 org.gradle.native 系统属性
- javascript - 如何将循环重写为匿名函数?
- linux - Linux x86-64 上的 Windows ARM 交叉编译
- docker - COPY 命令在多级 Dockerfile 中未按预期工作