Home > database >  pandas how to check if the following 2 rows have the same column value
pandas how to check if the following 2 rows have the same column value

Time:01-13

I have a df:

df = pd.DataFrame([[1,2], [9,0],[3,4], [3,4]], columns=list('AB'))

output:

    A   B
0   1   2
1   9   0
2   3   4
3   3   4

The length of the df is always even. I need to divide the rows by 2 ,it means ,first row compare with second row ,third row compare with forth row ,...,n-1 row compare with n row. My question is how to check if the column values of each 2 rows is exact the same.

For example, the first row and the second row are not the same ,but the third row and forth row are the same.

CodePudding user response:

Do you want:

df.iloc[::2].eq(df.iloc[1::2].values)

Output:

       A      B
0  False  False   # first row (index=0) vs second (index=1)
2   True   True   # third row (index=2) vs fourth (index=3)

To test equality of all columns:

df.iloc[::2].eq(df.iloc[1::2].values).all(axis=1)

Output:

0    False
2     True
dtype: bool

CodePudding user response:

df['pairs'] = df.index // 2
(df.groupby('pairs').apply('nunique') == 1).apply(all, axis=1)
  • Related