0

I have a Series

8 [11820] 9 [11820] 10 [11820] 11 [11820] 12 [11820] 27 [10599] 28 [10599] 29 [10599] 31 [661, 10599] 32 [661, 10599] 33 [7322] 34 [0] 37 [661] 39 [661] 40 [661] 49 [0, 661, 662, 663] 

I want to filter this Series with something like points[points.isin([0])] to get

34 [0] 49 [0, 661, 662, 663] 

but as a result I get 0 Features.

3
  • You are searching for lists. .isin() looks for values Commented Aug 10, 2018 at 9:12
  • @lhay86 yes, I want to find all features, which lists contain my value. Not only strict accordance Commented Aug 10, 2018 at 9:15
  • 1
    Possible duplicate of Python & Pandas: How to query if a list-type column contains something? Commented Aug 10, 2018 at 9:18

2 Answers 2

1

Simple way to check, if your value (0) is in the list, is by using apply on your series:

s = s[s.apply(lambda x: 0 in x)] 

Some explanation:
For every row it checks, whether 0 is in the list.

Apply returns a "True/False" series, where for every row True means that 0 is in the list inside the row.

After that your first series (s) is being filtered by this "True/False" series via [].

Sample code:

# This is your series s = pd.Series([[0], [11820], [11820], [10599], [0, 661, 662, 663]]) # This is the solution s = s[s.apply(lambda x: 0 in x)] # Print the result print(s) 0 [0] 4 [0, 661, 662, 663] Name: A, dtype: object 
Sign up to request clarification or add additional context in comments.

Comments

1

pd.Series.isin works by hashing and works on the entire element, i.e. it won't consider a partial match. Even for an exact match, since a list cannot be hashed, pd.Series.isin won't work with a series of lists.

Partial match

You can use a custom function with pd.Series.apply:

df = pd.DataFrame({'A': [[1, 2], [0], [0, 2, 3]]}) search_list = [0] # list of scalars mask = df['A'].apply(lambda x: any(i in x for i in search_list)) res = df[mask] print(res) A 1 [0] 2 [0, 2, 3] 

Exact match

You can convert your series to tuples, which are hashable, before any comparison. Then compare your series of tuples with a list of tuples.

search_list = [[0]] # list of lists mask = df['A'].map(tuple).isin(list(map(tuple, search_list))) res = df[mask] print(res) A 1 [0] 

Note operations with object dtype series will necessarily be inefficient. If possible, you should split your series of lists into multiple series of integers. Although, in this case, this may be cumbersome given the inconsistent list lengths.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.