python - Pandas:将列分组为时间序列
问题描述
考虑这组数据:
data = [{'Year':'1959:01','0':138.89,'1':139.39,'2':139.74,'3':139.69,'4':140.68,'5':141.17},
{'Year':'1959:07','0':141.70,'1':141.90,'2':141.01,'3':140.47,'4':140.38,'5':139.95},
{'Year':'1960:01','0':139.98,'1':139.87,'2':139.75,'3':139.56,'4':139.61,'5':139.58}]
如何转换为 Pandas 时间序列,如下所示:
Year Value
1959-01 138.89
1959-02 139.39
1959-03 139.74
...
1959-07 141.70
1959-08 141.90
...
解决方案
这是一种方法
s = pd.DataFrame(data).set_index("Year").stack()
s.index = pd.Index([pd.to_datetime(start, format="%Y:%m") + pd.DateOffset(months=int(off))
for start, off in s.index], name="Year")
df = s.to_frame("Value")
首先我们设置Year
为索引,然后将值堆叠在它旁边。然后通过可用日期 + 其他值作为月份偏移量从当前索引准备一个索引。最后转到新列名称为 的框架Value
。
要得到
>>> df
Value
Year
1959-01-01 138.89
1959-02-01 139.39
1959-03-01 139.74
1959-04-01 139.69
1959-05-01 140.68
1959-06-01 141.17
1959-07-01 141.70
1959-08-01 141.90
1959-09-01 141.01
1959-10-01 140.47
1959-11-01 140.38
1959-12-01 139.95
1960-01-01 139.98
1960-02-01 139.87
1960-03-01 139.75
1960-04-01 139.56
1960-05-01 139.61
1960-06-01 139.58
推荐阅读
- node.js - MongoDb 通过 _id 使用 Node 查询
- c++ - CMake复制DLL/将“bin”添加到共享库的路径
- r - 如何告诉 ddply 忽略 hms 而只查看 ymd?
- git - Jenkins在webhook触发时获取错误的分支
- html - 需要帮助理解嵌套
- python - 扩展Robotframework AppiumLibrary以调用appium execute_script函数时如何传递参数
- python - 从 numpy 中的数学表达式中删除 for 循环
- loops - 带有 2 个变量的 for 循环
- javascript - 这是我一段时间以来一直遇到的错误“找不到模块:无法解析'../lib'”,不知道问题是什么
- flutter - 如何从包含#标签的TextFormField中获取价值并管理它