Pandas comparing rows with a condition

Viewed 78

Let's say we have a example dataframe like below,

df = pd.DataFrame(np.array([['strawberry', 'red', 3], ['apple', 'red', 6], ['apple', 'red', 5],
                           ['banana', 'yellow', 9], ['pineapple', 'yellow', 5], ['pineapple', 'yellow', 7],
                           ['apple', 'green', 2],['apple', 'green', 6], ['kiwi', 'green', 6]
                           ]),
               columns=['Fruit', 'Color', 'Quantity'])

df

    Fruit       Color    Quantity
0   strawberry  red         3
1   apple       red         6
2   apple       red         5
3   banana     yellow       9
4   pineapple  yellow       5
5   pineapple  yellow       7
6   apple      green        2
7   apple      green        6
8   kiwi       green        6

In this df, I' m checking is there any change in Fruit column row by row.

With shift() method rows are offsetting by 1, with fillna() method NaN values are filled and lastly with ne() method True-False labeling is done.

So as you can check from index 1, strawberry changing to apple, it will be "True". Index 2, there are no change, it will be "False".

df['Fruit_Check'] = df.Fruit.shift().fillna(df.Fruit).ne(df.Fruit)
df
        Fruit        Color      Quantity    Fruit_Check
0     strawberry      red          3        False
1       apple         red          6        True
2       apple         red          5        False
3      banana        yellow        9        True
4     pineapple      yellow        5        True
5     pineapple      yellow        7        False
6       apple        green         2        True
7       apple        green         6        False
8       kiwi         green         6        True

My problem is: I want to check also "Color" column. If there is a change in there, Fruit_Check column must be False default. So df should look like this,

df
        Fruit        Color      Quantity    Fruit_Check
0     strawberry      red          3        False
1       apple         red          6        True
2       apple         red          5        False
3      banana        yellow        9        False
4     pineapple      yellow        5        True
5     pineapple      yellow        7        False
6       apple        green         2        False
7       apple        green         6        False
8       kiwi         green         6        True

Also I shouldn't use for loop. Because when I use with my original data, it takes too much time.

1 Answers

Use DataFrameGroupBy.shift for shift per groups:

df['Fruit_Check'] = df.groupby('Color').Fruit.shift().fillna(df.Fruit).ne(df.Fruit)
print (df)
        Fruit   Color Quantity  Fruit_Check
0  strawberry     red        3        False
1       apple     red        6         True
2       apple     red        5        False
3      banana  yellow        9        False
4   pineapple  yellow        5         True
5   pineapple  yellow        7        False
6       apple   green        2        False
7       apple   green        6        False
8        kiwi   green        6         True
Related