Syntax to select previous row in pandas after filtering Syntax to select previous row in pandas after filtering pandas pandas

Syntax to select previous row in pandas after filtering


Shift the mask up by 1.

df[(df['Value'] < 15).shift(-1).fillna(False)]   Row  Value1    2     25

More generally, if you're trying to find all rows greater than 15, whose next row is lesser than 15, you can compute two separate masks and AND them:

df[(df['Value'].shift(-1) < 15) & (df['Value'] > 15)]   Row  Value1    2     25


np.flatnonzero

To find where the mask is True then subtract one

df.iloc[np.flatnonzero(df.Value < 15) - 1]   Row  Value1    2     25


Using idxmax

>>> df.iloc[df.Value.le(15).idxmax() - 1]Row       2Value    25