我有一个数据框 df 看起来像这样 Date Value 10/1/2019 5 10/2/2019 10 10/3/2019 15 10/4/2019 20 10/5/2019 25 10/6/2019 30 10/7/2019 35我想计算 7 天的增量期望的输出:Date Delta10/1/2019 30这就是我正在做的事情:一位用户帮助我修改了以下代码: df['Delta']=df.iloc[0:,1].sub(df.iloc[6:,1]), Date=pd.Series (pd.date_range(pd.Timestamp('2019-10-01'), periods=7, freq='7d'))[['Delta','Date']]任何建议表示赞赏
1 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
让我们尝试一下shift
s = df.set_index('Date')['Value']
df['New'] = s.shift(freq = '-6 D').reindex(s.index).values
df['DIFF'] = df['New'] - df['Value']
df
Out[39]:
Date Value New DIFF
0 2019-10-01 5 35.0 30.0
1 2019-10-02 10 NaN NaN
2 2019-10-03 15 NaN NaN
3 2019-10-04 20 NaN NaN
4 2019-10-05 25 NaN NaN
5 2019-10-06 30 NaN NaN
6 2019-10-07 35 NaN NaN
添加回答
举报
0/150
提交
取消