Compute percent change with vaex dataframe

33 Views Asked by At

How can I compute the percent change of a column in a vaex dataframe when using the shift function?

import numpy as np
import vaex
df = vaex.from_arrays(x=np.arange(1,4))
df.x / df.shift(periods=1) - 1

results in an error:

  File "<unknown>", line 3
    1  1
       ^
SyntaxError: invalid syntax
1

There are 1 best solutions below

0
On BEST ANSWER

You need to make a copy of the x column and then shift it.

import numpy as np
import vaex
df = vaex.from_arrays(x=np.arange(1,4))
df['x_shifted'] = df.x
df = df.shift(periods=1,column='x_shifted')
df.x/df.x_shifted-1

enter image description here