I'm writing a function where I'd like to add a series to a dataframe that is True if n previous rows of another column are negative.
I have this working for a specific number as n, but I don't know how to generalize it into a parameter that could be passed in.
For example, we have a dataframe with a column named Total. The following code will put True in rows in a column titled consecutive_negative_value if there are 6 consecutive rows where Total is less than zero. How can this be generalized to accept any number n instead of just checking six periods?
df['negative_value'] = df['Total'] < 0
df['consecutive_negative_value'] = (
df['negative_value'] &
df['negative_value'].shift(1) &
df['negative_value'].shift(2) &
df['negative_value'].shift(3) &
df['negative_value'].shift(4) &
df['negative_value'].shift(5)
)