1 回答
TA贡献1883条经验 获得超3个赞
为了使用平均使用DataFrame.unstack
for进行轻松预测DatetimeIndex
,请添加下一个日期时间 by DataFrame.reindex
with date_range
,然后替换value1
级别中的缺失值 byDataFrame.fillna
并value2
设置for 1
,最后重新整形回来DataFrame.stack
:
print (df)
value1 value2
DT ID
2020-10-01 a 1 1
b 2 1
c 3 1
d 4 1
2020-10-02 a 10 1
b 11 1
c 12 1
d 13 1
rng = pd.date_range('2020-10-01','2020-10-04', name='DT')
df1 = df.unstack().reindex(rng)
df1['value1'] = df1['value1'].fillna(df1['value1'].mean())
df1['value2'] = 1
df2 = df1.stack()
print (df2)
value1 value2
DT ID
2020-10-01 a 1.0 1
b 2.0 1
c 3.0 1
d 4.0 1
2020-10-02 a 10.0 1
b 11.0 1
c 12.0 1
d 13.0 1
2020-10-03 a 5.5 1
b 6.5 1
c 7.5 1
d 8.5 1
2020-10-04 a 5.5 1
b 6.5 1
c 7.5 1
d 8.5 1
但预测比较复杂,你可以检查一下
添加回答
举报