python - Python通过复制日期索引将列合并为一列
问题描述
我想通过添加具有相同索引的行将列合并为一列。当然,索引是日期。然后我想绘制seaborn
情节。
我的数据:
df =
A B # A, B two unique groups, denote 36, 24 size
timestamp
2019-10-01 10 20
2019-10-02 30 40
预期答案:
df =
val group size # A, B denote 36, 24 size
timestamp
2019-10-01 10 A 34
2019-10-01 20 B 24
2019-10-02 30 A 34
2019-10-02 40 B 24
我的代码:
我不知道如何在这里进行。
解决方案
尝试melt
out = df.reset_index().melt('timestamp')
Out[73]:
timestamp variable value
0 2019-10-01 A 10
1 2019-10-02 A 30
2 2019-10-01 B 20
3 2019-10-02 B 40
推荐阅读
- php - 如何在 if(isset) 语句中使用 PHP 获取 URL 参数以更新表单
- php - Laravel 将数据传递到视图中
- c - issue with the using print function unable to print string
- java - 使用带有 GTFS 数据的协议缓冲区时无法克服“无效的线路类型”
- python - 如何以最小的时间间隔有效地找出两个数据帧中的共同元素?
- bash - bash脚本中没有空格的变量赋值背后的基本原理是什么
- c++ - 如何使用 OpenACC 对微分方程进行积分?
- node.js - 如何获取网站的 Node.js 源代码?
- javascript - 创建角色,然后将角色赋予成员不起作用,有没有更好的方法来处理这个问题?不和谐.js
- google-apps-script - 用于复制和重命名电子表格的 Google 表格脚本