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
#1
Good Morning !

I am tyring below code and expecting to get a boolean output 'True' for index B. But I am always getting false. I am trying to use pandas.isin() function.
How can I achieve it using isin() ?


code:
import pandas as pd

s = pd.Series(
                {
                    'A': [1,2,3,4,5],
                    'B': [6,7,8],
                    'C': [9,10,11,12]
                }


, name='My Pandas Series created using Dictionary list')
print('my new pandas series:\n',s)

# searching for a value "7" in series

print(s.isin([7]))
output:

Quote:A False
B False
C False
Reply
#2
That is correct. You have a series with three elements. None of the elements are 7.

import pandas as pd
This series has an element that is in [7]
print(pd.Series([6, 7, 8]).isin([7]))
Output:
0 False 1 True 2 False dtype: bool
What are you trying to do?
Reply
#3
(Apr-28-2023, 01:51 AM)deanhystad Wrote: That is correct. You have a series with three elements. None of the elements are 7.

import pandas as pd
This series has an element that is in [7]
print(pd.Series([6, 7, 8]).isin([7]))
Output:
0 False 1 True 2 False dtype: bool
What are you trying to do?



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:
Output:
A : False B: True C: False
Note: I was able to achieve desired output using combination of "pd.apply() and lambda()". Find below code:


print('\nusing different approach by using combination of apply() and lambda function to search value 7\n',my_series.apply(lambda x: 7 in x),'\n')


Output:
using different approach by using combination of apply() and lambda function to search value 7 A False B True C False
Reply
#4
I don't think isin is a good fit. Series.isin(list) is checking if elements in the series are in the list, not if elements in the list are in the series. Even if you could somehow get isin to work (say convert the Series you have into a Dataframe and then do isin on each Series in the dataframe), you would not get B: True, you would get B: False, True, False.

Again I ask "What are you trying to do?" This is an awkward use of pandas. Is that because it is a small part of a bigger picture where Pandas is effective, or is this using the wrong tool to solve a problem?
Reply
#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
#6
Thanks all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding element of specific field in pandas adjacency matrix amjass12 0 1,684 Oct-27-2020, 09:27 AM
Last Post: amjass12
  Convert quarterly time series to monthly time series donnertrud 1 5,191 May-22-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Alpha numeric element list search rhubarbpieguy 1 1,792 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  Unable to locate element no such element gahhon 6 4,519 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Change single element in 2D list changes every 1D element AceScottie 9 12,098 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  Python - Search element in list without the value index salmanfazal01 1 4,245 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