问题描述
在这个问题上,我会尽量保持清楚.假设我有一个数据框,格式为:
I will try to be as clear as possible in this question. Let's say I have a dataframe formed as:
Date Quantity
05/05/2017 34
12/05/2017 24
19/05/2017 45
26/05/2017 23
2/06/2017 56
9/06/2017 32
我想将此具有每周数据(如您所见)的数据框转换为具有每日数据的数据框.但是,我的数据框中(例如周末)会出现一些漏洞".我已经将每日日期存储到另一个变量中.我想获得这样的东西:
I would like to convert this dataframe having weekly data (as you see) into one having daily data. However, there will be some "holes" in my dataframe (ex. week-end days). I already have stored the daily dates into another variable.I would like to obtain something like this:
Date Quantity
05/05/2017 34
08/05/2017 34
09/05/2017 34
... ...
2/06/2017 56
5/06/2017 56
6/06/2017 56
... ...
我的想法是要有一个循环,其内容是:当日期早于每周数据帧中的日期(例如2017年5月19日)但高于前一个日期(所以12/05/2017)时,我想要在每日数据帧的数量"列中添加正确的数字(在本例中为45).
My idea is to have a loop that says "whenever the date is prior to the date in the weekly dataframe (ex. 19/05/2017) but higher than the previous date (so 12/05/2017), I want to append to the column "Quantity" in the daily dataframe the correct number (in this case 45).
但是,我不知道如何在Python中执行此操作.我应该将日期转换成数字,然后再转换回日期吗?有谁有更快的方法来做到这一点?非常感谢
However, I do not know how to do this in Python. Should I convert the dates into numbers and then back to dates? Does anyone have any faster way to do this? Thank you very much
推荐答案
以下是使用 resample
与 工作日频率 (B)并预先填写:
Here's an option using resample
with business day frequency (B) and forward fill:
df['Date'] = pd.to_datetime(df.Date, format='%d/%m/%Y')
df.set_index('Date').resample('B').ffill().reset_index()
# Date Quantity
#0 2017-05-05 34
#1 2017-05-08 34
#2 2017-05-09 34
#...
#20 2017-06-02 56
#21 2017-06-05 56
#22 2017-06-06 56
#...
这篇关于使用Python将每周数据转换为每日数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!