'How to add values one by one in a new column with pandas

I have a column with acceleration values, and I’m trying to integrate them in a new column. Here’s what I want as output :

  A B
0 a b-a
1 b c-b
2 c d-c
3 d …-d
…

I’m currently doing like that

l=[]
for i in range(len(df)):
   l.append(df.values[i+1][0]-df.values[i][0])
df[1]=l

That’s very slow to process. I have over a million lines, and this in 20 different csv files. Is there a way to do it faster ?



Solution 1:[1]

IIUC, you can use diff:

df = pd.DataFrame({'A': [0,2,1,10]})
df['B'] = -df['A'].diff(-1)

output:

    A    B
0   0  2.0
1   2 -1.0
2   1  9.0
3  10  NaN

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway