[英]Python Converting Quarterly to Monthly Data
0 1Q08
1 2Q08
2 3Q08
3 4Q08
4 1Q09
5 2Q09
Currently My dataframe looks like this. 目前,我的数据框看起来像这样。 I would love to convert the quaterly to monthly like this:
我很乐意将季度转换为每月,如下所示:
0 01-01-08
1 02-01-08
2 03-01-08
3 04-01-08
4 05-01-08
5 06-01-08
It requires adding two rows per each quarter and converting a quarter into three months. 它要求每个季度添加两行,并将一个季度转换为三个月。 Is there a built-in function for this?
是否有内置功能? If not, what would be an efficient way?
如果没有,什么是有效的方法? Thanks!
谢谢!
There are probably many ways of doing this. 可能有很多方法可以做到这一点。 I find split() really useful for reformatting.
我发现split()对于重新格式化确实很有用。
i = 0
for d in data_frame:
quarter, year = d.split("\t")[1].split("Q")
for j in range(3):
month = (int(quarter) - 1) * 3 + j + 1
print "%s\t%s-01-%s" % (i, month, year)
i += 1
The '%s' line is an example of python's string substitution syntax. '%s'行是python的字符串替换语法的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.