Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if values in all n previous rows are greater than the value of current row

I have a pandas dataframe like this:

    col_name
0      2
1      3
2      1
3      0
4      5
5      4
6      3
7      3

that could be created with the code:

import pandas as pd

dataframe = pd.DataFrame(
    {
        'col_name': [2, 3, 1, 0, 5, 4, 3, 3]
    }
)

Now, I want to get the rows which have a value less than the values in all the n previous rows. So, for n=2 the output is the rows: 2, 3, 6.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Additionally I don’t want to use any for-loops in my code.

Just a trick that comes to my mind is that we can count the number of rows in n previous rows having a value less than the current row and then check if the number equals to n.

Have you any idea for the code with my trick or in any other ways? and also is there a way to write a similar code except that if we want to check if values in n next rows are less than the value of the current row?

>Solution :

Let us use rolling to calculate the min value in n previous rows then compare the min value with current row to create a boolean mask

df[df['col_name'] < df['col_name'].shift().rolling(2, min_periods=1).min()]

   col_name
2         1
3         0
6         3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading