背景
我需要使用Pandas编写一个算法,从第二行开始,将一列值减去上一行的值,并使用结果继续减去下一行的值,以此类推。
示例
INPUT:
ID VALUE
0 1
1 10
2 30
3 45
4 78
OUTPUT (just the result, not the operation itself):
ID VALUE
0 1
1 9 #(10-1)
2 21 #(30-9)
3 24 #(45-21)
4 54 #(78-24)
我所尝试的
df['VALUE'] = df['VALUE'] - df['VALUE]'.shift() # Doesn't starts with the second row, and use the original dataframe to subtract
df['VALUE'] = df['VALUE'].diff() # Doesn't starts with the second row, and use the original dataframe to subtract