0

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 ?

bappi
  • 137
  • 9
  • I'm sure that's possible with vectorized operations. Can't say anything else given the information you provided. – timgeb May 05 '22 at 08:23
  • @timgeb dont really know what else to add, i think my question is clear, and I couldn’t find any answer or help anywhere – bappi May 05 '22 at 08:25
  • Your question has no sample data, no code, no actual output and no desired output. It could be much, much clearer. – timgeb May 05 '22 at 08:26
  • See [How to make good reproducible pandas examples](https://stackoverflow.com/a/20159305/3620003). – timgeb May 05 '22 at 08:28

1 Answers1

0

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
mozway
  • 194,879
  • 13
  • 39
  • 75