Python Forum
search an element in pandas series
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search an element in pandas series
#5
(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: bool
Instead 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.
Reply


Messages In This Thread
RE: search an element in pandas series - by perfringo - Apr-28-2023, 08:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  finding element of specific field in pandas adjacency matrix amjass12 0 1,706 Oct-27-2020, 09:27 AM
Last Post: amjass12
  Convert quarterly time series to monthly time series donnertrud 1 5,258 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Alpha numeric element list search rhubarbpieguy 1 1,830 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  Unable to locate element no such element gahhon 6 4,580 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Change single element in 2D list changes every 1D element AceScottie 9 12,180 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  Python - Search element in list without the value index salmanfazal01 1 4,300 Nov-04-2016, 02:07 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020