我有这个数据框:
list()
我想每天计算同一时间戳中两行之间的差异,然后将新计算的数据添加到新列中,如下所示:
date_time value
1/10/2016 0:00:00 28.4
1/10/2016 0:05:00 28.4
1/10/2016 0:10:00 28.4
1/11/2016 0:00:00 27.4
1/11/2016 0:05:00 27.4
1/11/2016 0:10:00 27.4
我不想添加包含最后一天数据的新列,然后计算更改。还有其他办法吗?提前谢谢!
答案 0 :(得分:0)
您可以使用:
#convert column to datetime and create index
df = df.set_index(pd.to_datetime(df['date_time']))
#shift by 1 day and reindex for same indices, subtract from right by rsub
df['change24h'] = df['value'].shift(freq='1D').reindex(df.index).rsub(df['value'])
#same as
#df['change24h'] = df['value'].sub(df['value'].shift(freq='1D').reindex(df.index))
#remove datetime index
df = df.reset_index(drop=True)
print (df)
date_time value change24h
0 1/10/2016 0:00:00 28.4 NaN
1 1/10/2016 0:05:00 28.4 NaN
2 1/10/2016 0:10:00 28.4 NaN
3 1/11/2016 0:00:00 27.4 -1.0
4 1/11/2016 0:05:00 29.4 1.0
5 1/11/2016 0:10:00 28.4 0.0