python - Pandas DataFrame 按不等列顺序旋转
问题描述
我有一个看起来像这样的数据框:
df=pd.DataFrame({'Machine':['A','A','B','B','B'],
'Date':['1/1/2020','4/5/2020','4/1/2020','4/3/2020','5/1/2020'],
'Production':[5,9,44,13,34],
})
Machine Date Production
0 A 1/1/2020 5
1 A 4/5/2020 9
2 B 4/10/2020 44
3 B 4/3/2020 13
4 B 5/1/2020 34
我希望将其转换为:
A B
0 5 13
1 9 44
2 34
索引只是按顺序排列的日期排名。
有没有办法在不先创建排名列的情况下做到这一点?
raw_data["RANK"] = raw_data.groupby('Machine')["Date"].rank(method="first", ascending=True)
new_data = raw_data.pivot(columns = 'Machine', values = 'Production', index = 'RANK')
解决方案
推荐阅读
- java - Hibernate 如何在底层以 OneToMany 关系获取数据?
- swift - 如何在 iOS Swift 中获取打开蓝牙的设备的名称
- ubuntu - 在 Ubuntu 18 上安装 Xcas 时出错 - mpfr 版本错误
- python - Python:如何从网站上抓取信息?
- pip - 安装该特定模块后的 ModuleNotFoundError
- python - Numpy矩阵幂函数和矩阵乘法
- kotlin - Kotlin map 函数返回一个 Stream 并且不是 collect() 的 Java 等价物
- javascript - 使用 lodash 检查对象是否包含数组中的值
- python - 如何在Python中获取正则表达式后剩余字符串索引的索引?
- events - Blazor:从服务发送到组件的事件为空