I'm looping through a DataFrame and want to check whether three conditions are met in the previous 150 records but not the last 30 or so.
The columns I'm wanting to check are TRAJ50_1, TRAJ100_1, TRAJ200_1 (where all have values less than 0)
I'm currently trying the following:
for i in range(len(values)):
if i > 0:
if data.loc[i-150:i-30, (data['TRAJ50_1'][i] < 0) & (data['TRAJ100_1'] < 0) & (data['TRAJ200_1'] < 0)]:
_recent_decline = 1
else:
_recent_decline = 0
This is resulting in the following error:
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
I've spent hours searching but as a relative newbie to Python its a tad confusing. Thank you in advance.
CodePudding user response:
try replacing
if data.loc[i-150:i-30, (data['TRAJ50_1'][i] < 0) & (data['TRAJ100_1'] < 0) & (data['TRAJ200_1'] < 0)]:
with:
if data.iloc[i-150:i-30][(data['TRAJ50_1'][i] < 0) & (data['TRAJ100_1'] < 0) & (data['TRAJ200_1'] < 0)]:
CodePudding user response:
Another approach:
# s is True if all three columns are negative for the row
s = df[["TRAJ50_1", "TRAJ100_1", "TRAJ200_1"]].lt(0).all(axis=1)
# If there are any instances of s being True between 30 and 150 previous rows,
# set is_recent_decline to True
is_recent_decline = (s.rolling(150, closed="left").sum() - s.rolling(30, closed="left").sum()) > 0