Home > Enterprise >  Python - Check values in consecutive columns(i.e across rows)
Python - Check values in consecutive columns(i.e across rows)

Time:10-11

I have a 6x4 dataframe containing numerical values. I would like to check if the value in the current column is the same as the next column's i.e are there any equal values in consecutive columns per row?. How do I perform this check as a new column?

import itertools as it
import pandas as pd

list(set(it.permutations([1,1,0,0])))

x_list = list(set(it.permutations([1,1,0,0])))

x_df =  pd.DataFrame(x_list)

x_df.columns = ['one', 'two', 'three', 'four']

CodePudding user response:

If I understood you correctly:

x = x_df.diff(periods=-1, axis=1)
x['four'] = x_df['four'] - x_df['three']
print((x==0))

Input:

   one  two  three  four
0    1    0      1     0
1    1    1      0     0
2    1    0      0     1
3    0    1      1     0
4    0    1      0     1
5    0    0      1     1

Output:

     one    two  three   four
0  False  False  False  False
1   True  False   True   True
2  False   True  False  False
3  False   True  False  False
4  False  False  False  False
5   True  False   True   True
  • Related