Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to select previous row in pandas after filtering

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!

like image 496
Adrian Y Avatar asked Jun 26 '18 02:06

Adrian Y


People also ask

How do I select the last row in pandas?

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.

How do I select the bottom row in pandas?

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.

Which syntax is correct for filter in pandas?

DataFrame - filter() function.


2 Answers

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
like image 59
cs95 Avatar answered Oct 19 '22 08:10

cs95


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
like image 30
piRSquared Avatar answered Oct 19 '22 06:10

piRSquared