36

What is the python equivalent of this in operator? I am trying to filter down a pandas database by having rows only remain if a column in the row has a value found in my list.

I tried using any() and am having immense difficulty with this.

4
  • 1
    @Jeff post that as an answer. Commented Aug 8, 2014 at 15:00
  • That's beautiful, exactly what I was looking for. You know how hard it is to google "in" and special symbols. Commented Aug 8, 2014 at 15:01
  • I don't see the difficulty. Googling "pandas in operator" provides pandas.pydata.org/pandas-docs/stable/indexing.html as first hit and a text search of "in operator" on that page let's you immediately find what you are looking for. Commented Aug 8, 2014 at 21:21
  • I googled Python rather than pandas, I didn't know it was a Pandas specific thing. Commented Aug 8, 2014 at 21:28

4 Answers 4

52

Pandas comparison with R docs are here.

s <- 0:4 s %in% c(2,4) 

The isin method is similar to R %in% operator:

In [13]: s = pd.Series(np.arange(5),dtype=np.float32) In [14]: s.isin([2, 4]) Out[14]: 0 False 1 False 2 True 3 False 4 True dtype: bool 
Sign up to request clarification or add additional context in comments.

Comments

13

FWIW: without having to call pandas, here's the answer using a for loop and list compression in pure python

x = [2, 3, 5] y = [1, 2, 3] # for loop for i in x: [].append(i in y) Out: [True, True, False] # list comprehension [i in y for i in x] Out: [True, True, False] 

Comments

2

If you want to use only numpy without panads (like a use case I had) then you can:

import numpy as np x = np.array([1, 2, 3, 10]) y = np.array([10, 11, 2]) np.isin(y, x) 

This is equivalent to:

c(10, 11, 2) %in% c(1, 2, 3, 10) 

Note that the last line will work only for numpy >= 1.13.0, for older versions you'll need to use np.in1d.

Comments

-2

As others indicate, in operator of base Python works well.

myList = ["a00", "b000", "c0"] "a00" in myList # True "a" in myList # False 

1 Comment

But requires an atomic left side to yield results that match R's %in% in calling semantics. E.g. ["a00", "node", "c0"] in myList isn't what someone used to %in% is going to expect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.