Home > Software engineering >  How to add values one by one in a new column with pandas
How to add values one by one in a new column with pandas

Time:05-05

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 ?

CodePudding user response:

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
  • Related