Search code examples
pythonpandas

extract rows with change in consecutive values of a column from pandas dataframe


I am trying to extract the rows where consecutive value changes for a particular column

Ex:

sno A   B
1   a   yes
2   b   yes
3   c   No
4   d   No
5   e   No
6   f   yes
7   g   yes
8   h   yes
9   I   No

Output:

sno A   B
        
2   b   yes
3   c   No
5   e   No
6   f   yes     
8   h   yes
9   I   No

Consecutive column is B, am trying to extract if the value of column B is changed consecutively


Solution

  • Another possible solution:

    d = df['B'].shift().ne(df['B']) 
    e = ~d & d.shift(-1)
    df[e | e.shift()]
    

    Intermediates:

    d:

    0     True
    1    False
    2     True
    3    False
    4    False
    5     True
    6    False
    7    False
    8     True
    Name: B, dtype: bool
    

    e:

    0    False
    1     True
    2    False
    3    False
    4     True
    5    False
    6    False
    7     True
    8    False
    Name: B, dtype: bool
    

    Output:

       sno  A    B
    1    2  b  yes
    2    3  c   No
    4    5  e   No
    5    6  f  yes
    7    8  h  yes
    8    9  I   No