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

How to compare rows within a dataframe column and check if value is changing?

I have a data frame as seen:

enter image description here

How to check conditions when results change from ‘NO’ to ‘OK’ and if it does populate the next column as true. Results should look like this:

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

enter image description here

>Solution :

My understanding question is need test OK if duplicated group only, so here are solutions with more data:

#test OK
m = df['Result'].eq('OK')
#first values per groups by consecutive values
m1 = m.ne(m.shift())
#chained consecutive OK with first values per groups
df['check1'] = m1.cumsum().duplicated(keep=False) & m1

#chained OK if from NO to OK with consecutive OK
df['check2'] = (m & df['Result'].shift().eq('NO')) & m1.cumsum().duplicated(keep=False)
print (df)
   Result  check1  check2
0      OK   False   False
1      NO   False   False
2      OK    True    True
3      OK   False   False
4      NO   False   False
5      OK    True    True
6      OK   False   False
7      OK   False   False
8      NO   False   False
9      OK   False   False
10     NO   False   False
11     OK   False   False
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