How to apply a log function between two consecutives lines in pandas dataframe

Viewed 41

In a dataframe that contains prices on Brent per day, I am trying to apply a log function between two consecutive days. I guess the answer is simple but I couldn't find it.

ln[X(i+1)/X(i)] for each line

img

Thanks in advance for your help

1 Answers

You can use:

np.log(X.shift(-1) / X)

which shifts the data 1 step back (hence the minus) to get X(i+1) and then divides it by X(i). Lastly, take the log.

Notes:

  • The last row will be NaN because there is no i+1th data for them
  • np.log is the natural logarithm not log10
Related