Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of the R operator "%in%"

Tags:

python

pandas

r

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.

like image 659
wolfsatthedoor Avatar asked Aug 08 '14 14:08

wolfsatthedoor


Video Answer


1 Answers

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 
like image 87
Jeff Avatar answered Oct 06 '22 05:10

Jeff