Search code examples
pythonpandasnumpydata-analysis

Compare elements in multiple pandas Series, given as a list


I have a multiple Series, given as a list. Number of series may vary.

s1 = pandas.Series(data=['Bob', 'John', '10', 10, 'i'])
s2 = pandas.Series(data=['John', 'John', 10, 10, 'j'])
s3 = pandas.Series(data=['Bob', 'John', '10', 10, 'k'])
series = [s1,s2,s3] 

What I want is to check list with a series if elements are equal and get back list with an indexes or numpy.array with booleans.

What I have tried:

numpy.equal.reduce([s for s in series])

or

numpy.equal.reduce([s.values for s in series])

But with a given series i get:

array([ True,  True,  True,  True,  True])

I expected:

array([ False,  True,  False,  True,  False])

Are there any elegant way to do this job, without constructing big iterating methods?

Thank you!


Solution

  • You can simply construct a df and check number of unique:

    print (pd.DataFrame(series).nunique().eq(1))
    
    0    False
    1     True
    2    False
    3     True
    4    False
    dtype: bool
    

    Or as an array:

    print (pd.DataFrame(series).nunique().eq(1).to_numpy())
    
    [False  True False  True False]