Bryan Fok Bryan Fok - 3 months ago 32
Python Question

pandas dataframe look ahead value

What's the best way to do this with pandas dataframe? I want to loop through a dataframe, and compute the difference between the current value and the next value which is different than the current value.
For example:
[13, 13, 13, 14, 13, 12]
will create a new column with this
[-1, -1, -1, 1, 1]

Answer

How about use diff to calculate the difference and then back fill 0 with the next non zero value:

import pandas as pd
import numpy as np
df = pd.DataFrame({"S": [13, 13, 13, 14, 13, 12]})

df.S.diff(-1).replace(0, np.nan).bfill()  # replace zero with nan and apply back fill.

# 0    -1
# 1    -1
# 2    -1
# 3     1
# 4     1
# 5   NaN
# Name: S, dtype: float64