Shakti Shakti - 1 year ago 81
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 Source

You could use pd.rolling_max for this, something like

window = 5
pd.rolling_max(df['Close'], window).shift(-window) - df['Close'])  > 0 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download