(Apr-28-2023, 03:29 AM)learningPython Wrote: Agree. isin() is searching for element 7 while each element in my series is a LIST. Hence returning false.
Is there a way, we can use somehow still use isin() to return following:
You know that every element in series is list. Does list have
isin
method?Straightforward approach to achieve desired result could be with helper function:
import pandas as pd s = pd.Series( { 'A': [1,2,3,4,5], 'B': [6,7,8], 'C': [9,10,11,12] }) def helper(item): return 7 in item print(s.apply(helper)) A False B True C False dtype: boolInstead helper function lambda can be used:
print(s.apply(lambda item: 7 in item))Of course,
isin
can be "used" as well: "explode series, compare, group boolean values back by index, find if any in group is true":print(s.explode().isin([7]).groupby(level=0).apply(any)) A False B True C False dtype: bool
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.