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

Time:12-17

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:

enter image description here

CodePudding user response:

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

CodePudding user response:

Try:

df['check'] = df['Result'].eq('OK') & df['Result'].shift().eq('NO')
print(df)

# Output:
  Result  check
0     NO  False
1     OK   True
2     OK  False
3     NO  False
4     OK   True
5     OK  False
6     OK  False
7     NO  False

To display correctly:

df['check'] = df['check'].replace({True: 'TRUE', False: ''})
print(df)

# Output:
  Result check
0     NO      
1     OK  TRUE
2     OK      
3     NO      
4     OK  TRUE
5     OK      
6     OK      
7     NO      
  • Related