Python Forum
search an element in pandas series - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: search an element in pandas series (/thread-39879.html)



search an element in pandas series - learningPython - Apr-27-2023

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



RE: search an element in pandas series - deanhystad - Apr-28-2023

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?


RE: search an element in pandas series - learningPython - Apr-28-2023

(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



RE: search an element in pandas series - deanhystad - Apr-28-2023

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?


RE: search an element in pandas series - perfringo - Apr-28-2023

(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



RE: search an element in pandas series - learningPython - Apr-30-2023

Thanks all.