Bryan Fok
Bryan Fok

Reputation: 3487

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]

Upvotes: 0

Views: 808

Answers (1)

akuiper
akuiper

Reputation: 215107

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

Upvotes: 2

Related Questions