python - 根据 Python pandas 中的系列动态填充缺失的年份和星期值
问题描述
有一个 2 列的 csv。该文件包含一些缺失的基于系列的年-周值。
输入:-
Date A
2019-51 10
2020-04 20
输出:-
Date A
2019-51 10
2019-52 10
2020-01 10
2020-02 10
2020-03 10
2020-04 20
我需要 pandas python 代码来生成上述输出
解决方案
我们使用的 IIUCresample
df.index=pd.to_datetime(df.Date+'0',format = '%Y-%W%w')
df=df.resample('W').ffill()
df.index=df.index.strftime('%Y-%W')
df=df.drop('Date',1).reset_index()
df
Out[57]:
index A
0 2019-51 10
1 2020-00 10# this not ISO week
2 2020-01 10
3 2020-02 10
4 2020-03 10
5 2020-04 20
如果你想从 01
df.index=pd.to_datetime(df.Date+'0',format = '%G-%V%w')
df=df.resample('W').ffill()
df.index=df.index.strftime('%Y-%V')
df=df.drop('Date',1).reset_index()
df
Out[62]:
index A
0 2019-51 10
1 2019-52 10
2 2020-01 10
3 2020-02 10
4 2020-03 10
5 2020-04 20
推荐阅读
- wicket - Apache Wicket 1.6 或更高版本不支持 visual-wicket
- selenium - 在 python 中运行 Geckodriver 时出错:[Errno 2] 没有这样的文件或目录:'geckodriver.log'
- android - 如何打开封闭的房间数据库
- java - 如何使用 protobuf 编译 java 特定枚举
- javascript - 如何在javascript上为烛台顶点图添加注释
- node.js - 在createReadStream nodejs中调用函数
- php - Laravel刀片文件到word文档
- asp.net-mvc-5 - 如何解决:无法创建 mvc 项目
- html - 当他们得到一个新孩子时,内联块 div 的位置会变得混乱
- java - 离开应用程序时,SharedPreferences 必须被删除