I have a pd.DataFrame df
with 5 lines, say:
Row Value
1 32
2 25
3 10
4 18
5 21
Lets say I use the filter df[df['Value'] < 15]
and this should return
Row Value
3 10
My question is, I want to access the value for the row before this filter returns True, in this example I want to function to return the value 25 (from Row 2).
My end goal is to apply the condition, get the row above, and test this row for an additional condition.
What pandas functions can I use?
Thanks!
Select & print last row of dataframe using tail() It will return the last row of dataframe as a dataframe object. Using the tail() function, we fetched the last row of dataframe as a dataframe and then just printed it.
Method 1: Using tail() method DataFrame. tail(n) to get the last n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the end). By default n = 5, it return the last 5 rows if the value of n is not passed to the method.
DataFrame - filter() function.
Shift the mask up by 1.
df[(df['Value'] < 15).shift(-1).fillna(False)]
Row Value
1 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 Value
1 2 25
np.flatnonzero
To find where the mask is True
then subtract one
df.iloc[np.flatnonzero(df.Value < 15) - 1]
Row Value
1 2 25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With