Shakti Shakti - 1 month ago 9
Python Question

How to check for a range of values in a pandas dataframe?

I have a pandas dataframe in which I want to check if a future value is greater than the given value for a range of future dates.
For example

np.where((df['Close'].shift(-1) - df['Close']) > 0 , 1, 0)


This will give me if next value is greater than current value, I want to check if current value is greater than say next 5 values ?

Answer

You could use pd.rolling_max for this, something like

window = 5
pd.rolling_max(df['Close'], window).shift(-window) - df['Close'])  > 0