我有一个如下的数据框:
df = pd.DataFrame({'price': ['480,000,000','477,000,000', '608,700,000', '580,000,000', '350,000,000'], 'sale_date': ['1396/10/30','1396/10/30', '1396/11/01', '1396/11/03', '1396/11/07']})
df
Out[7]:
price sale_date
0 480,000,000 1396/10/30
1 477,000,000 1396/10/30
2 608,700,000 1396/11/01
3 580,000,000 1396/11/04
4 350,000,000 1396/11/04
所以我定义了 period datetime 并按天重新采样
df['sale_date']=df['sale_date'].str.replace('/','').astype(int)
df['price'] = df['price'].str.replace(',','').astype(int)
def conv(x):
return pd.Period(year=x // 10000,
month=x // 100 % 100,
day=x % 100, freq='D')
df['sale_date'] = df['sale_date'].apply(conv)
s = df.groupby('sale_date')['price'].sum()
那么我想通过前一天的值来填补日期时间的空白。
这是我想要的输出:
In [13]:
price sale_date
0 957,000,000 1396/10/30
2 608,700,000 1396/11/01
0 680,000,000 1396/10/02
0 680,000,000 1396/10/03
3 930,000,000 1396/11/04
或通过前一天和第二天
所需的输出:
In [13]:
price sale_date
0 957,000,000 1396/10/30
2 608,700,000 1396/11/01
0 769,000,000 1396/10/02
0 769,000,000 1396/10/03
3 930,000,000 1396/11/04