Pandas Dataframe Find Rows Where all Columns Equal

Question:

I have a dataframe that has characters in it – I want a boolean result by row that tells me if all columns for that row have the same value.

For example, I have

df = [  a   b   c   d

0  'C'   'C'   'C'   'C' 

1  'C'   'C'   'A'   'A'

2  'A'   'A'   'A'   'A' ]

and I want the result to be

0  True

1  False

2  True

I’ve tried .all but it seems I can only check if all are equal to one letter. The only other way I can think of doing it is by doing a unique on each row and see if that equals 1? Thanks in advance.

Asked By: Lisa L

||

Answers:

I think the cleanest way is to check all columns against the first column using eq:

In [11]: df
Out[11]: 
   a  b  c  d
0  C  C  C  C
1  C  C  A  A
2  A  A  A  A

In [12]: df.iloc[:, 0]
Out[12]: 
0    C
1    C
2    A
Name: a, dtype: object

In [13]: df.eq(df.iloc[:, 0], axis=0)
Out[13]: 
      a     b      c      d
0  True  True   True   True
1  True  True  False  False
2  True  True   True   True

Now you can use all (if they are all equal to the first item, they are all equal):

In [14]: df.eq(df.iloc[:, 0], axis=0).all(1)
Out[14]: 
0     True
1    False
2     True
dtype: bool
Answered By: Andy Hayden
df = pd.DataFrame.from_dict({'a':'C C A'.split(),
                        'b':'C C A'.split(),
                        'c':'C A A'.split(),
                        'd':'C A A'.split()})
df.apply(lambda x: len(set(x)) == 1, axis=1)
0     True
1    False
2     True
dtype: bool

Explanation: set(x) has only 1 element, if all elements of the row are the same. The axis=1 option applies any given function over the rows instead.

Answered By: K.-Michael Aye

nunique: New in version 0.20.0.(Base on timing benchmark from Jez , if performance is not important you can using this one)

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

Or you can using map with set

list(map(lambda x : len(set(x))==1,df.values))
Answered By: BENY

Compare array by first column and check if all Trues per row:

Same solution in numpy for better performance:

a = df.values
b = (a == a[:, [0]]).all(axis=1)
print (b)
[ True  True False]

And if need Series:

s = pd.Series(b, axis=df.index)

Comparing solutions:

data = [[10,10,10],[12,12,12],[10,12,10]]
df = pd.DataFrame(data,columns=['Col1','Col2','Col3'])

#[30000 rows x 3 columns]
df = pd.concat([df] * 10000, ignore_index=True)

#jez - numpy array
In [14]: %%timeit
    ...: a = df.values
    ...: b = (a == a[:, [0]]).all(axis=1)
141 µs ± 3.23 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

#jez - Series 
In [15]: %%timeit
    ...: a = df.values
    ...: b = (a == a[:, [0]]).all(axis=1)
    ...: pd.Series(b, index=df.index)
169 µs ± 2.02 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

#Andy Hayden
In [16]: %%timeit
    ...: df.eq(df.iloc[:, 0], axis=0).all(axis=1)
2.22 ms ± 68.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

#Wen1
In [17]: %%timeit
    ...: list(map(lambda x : len(set(x))==1,df.values))
56.8 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

#K.-Michael Aye
In [18]: %%timeit
    ...: df.apply(lambda x: len(set(x)) == 1, axis=1)
686 ms ± 23.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

#Wen2    
In [19]: %%timeit
    ...: df.nunique(1).eq(1)
2.87 s ± 115 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Answered By: jezrael

You can use nunique(axis=1) so the results (added to a new column) can be obtained by:

df['unique'] = df.nunique(axis=1) == 1

The answer by @yo-and-ben-w uses eq(1) but I think == 1 is easier to read.

Answered By: Duke
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.