user47091
user47091

Reputation: 543

How can I compare rows for multiple columns if they are identical

I have one pandas DataFrame and I want to check the rows are identical. This shall work for 3 columns out of the dataframe.

A B C ...
1 1 1 ...
1 2 3 ...
1 4 4 ...

This should bring up a mask with

True
False
False

Upvotes: 2

Views: 43

Answers (3)

piRSquared
piRSquared

Reputation: 294546

Using diff

df.diff(axis=1).fillna(0).eq(0).all(1)

0     True
1    False
2    False
dtype: bool

Using shift

df.eq(df.shift(axis=1)).iloc[:, 1:].all(1)

0     True
1    False
2    False
dtype: bool

Using std (inspired by @Wen)

df.std(1).eq(0)

0     True
1    False
2    False
dtype: bool

Inspired by @MaxU with a healthy amount of obnoxious sprinkled in.

(lambda v, j: pd.Series((v == v[:, [0]]).all(1), j))(df.values, df.index)

0     True
1    False
2    False
dtype: bool

Now with less obnoxious

v = df.values
pd.Series((v == v[:, [0]]).all(1), df.index)

Upvotes: 3

BENY
BENY

Reputation: 323396

Or you can use nunique

df.nunique(1).eq(1)
Out[1155]: 
0     True
1    False
2    False
dtype: bool

Upvotes: 4

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

In [97]: df.eq(df.iloc[:, 0], axis=0).all(1)
Out[97]:
0     True
1    False
2    False
dtype: bool

Upvotes: 5

Related Questions