How to check if a tuple value exists in a DataFrame column

Viewed 356

I have a column in a DataFrame which contains either strings or tuples of strings. I would like to check whether a certain tuple of values is present in that column.

s = pd.Series(['a', ('b', 'c', 'd')])

'a' in s.values

This returns True. However:

('b', 'c', 'd') in s.values

returns False, with a warning: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison.

How can I work around this?

2 Answers

Here you go:

s = pd.Series(['a', ('b', 'c', 'd')])
def check_tuple(x, tuple_string="('b', 'c', 'd')"):
    if str(x).find(tuple_string)>=0:
        return True
s.apply(check_tuple)

Get all hash values of objects in your serie, and compare it to the hash value you looking for

Related