user2242044
user2242044

Reputation: 9213

Using pd.apply() with shift to compare adjacent rows

I'd like to compare adjacent rows on two separate columns. I have a dataframe with two columns, Letter and Boolean_Value. I would like to create a new column, New Col in which the value is True or False.

For the value to be true:

The next row down (higher index) must share the same Letter value and the the next row down must have a Boolean_Value of True.

for the value to be false:

All other scenarios

My code, yields all false values:

import pandas as pd
df = pd.DataFrame(data=[['A', False],
                        ['A', True],
                        ['B', False],
                        ['B', False],
                        ['B', True]]
                  ,columns=['Letter', 'Boolean_Value'])

def my_test2(row):
    try:
        if row['Letter'].shift(1) == row['Letter'] and row['Boolean_Value'].shift(1) == True:
            return True
        else:
            return False
    except:
        return False

df['New Col'] = df.apply(my_test2, axis=1)

print df

Yields:

  Letter Boolean_Value New Col
0      A         False   False
1      A          True   False
2      B         False   False
3      B         False   False
4      B          True   False

Desired output:

  Letter Boolean_Value New Col
0      A         False   True
1      A          True   False
2      B         False   False
3      B         False   True
4      B          True   False

Upvotes: 1

Views: 1021

Answers (2)

Blind0ne
Blind0ne

Reputation: 1037

Try this:

import pandas as pd
df = pd.DataFrame(data=[['A', False],
                        ['A', True],
                        ['B', False],
                        ['B', False],
                        ['B', True]]
                  ,columns=['Letter', 'Boolean_Value'])


df['New Col'] = df['Letter'] == df['Letter'].shift(-1)
df['New Col'] = df['New Col'] != df['Boolean_Value'].shift(1)

print(df)

Upvotes: 1

Stefan
Stefan

Reputation: 42885

You could:

df['new_col'] = (df.Letter == df.Letter.shift(-1)) & df.Boolean_Value.shift(-1)

  Letter Boolean_Value new_col
0      A         False    True
1      A          True   False
2      B         False   False
3      B         False    True
4      B          True   False

Upvotes: 2

Related Questions