-1

What is the pandas equivalent of the R function %in% ?

When we have a dataframe in R, we can check for which rows a column contains strings from a list using the operator %in% which gives a Boolean output.

Concrete example: If we want to check which rows the strings "setosa" and "virginica" are in the column species of the iris dataset, we can simply use the following code:

iris[:,c('species')] %in% c('setosa', 'virginica').

How can we do the same thing in python for a pandas DataFrame?

The reason I want to do this is I want to filter the dataset and only keep rows with the species "setosa" or "virginica".

2 Answers 2

2

%in% in R is actually is.element:

r$> 1 %in% 1:2                    
[1] TRUE

r$> is.element(1, 1:2)                                  
[1] TRUE

datar has ported some functions in R to python:

>>> from datar.all import c, f, is_element, filter
>>> from datar.datasets import iris
>>> 
>>> iris >> filter(is_element(f.Species, c('setosa', 'virginica')))
    Sepal_Length  Sepal_Width  Petal_Length  Petal_Width    Species
       <float64>    <float64>     <float64>    <float64>   <object>
0            5.1          3.5           1.4          0.2     setosa
1            4.9          3.0           1.4          0.2     setosa
2            4.7          3.2           1.3          0.2     setosa
3            4.6          3.1           1.5          0.2     setosa
..           ...          ...           ...          ...        ...
4            5.0          3.6           1.4          0.2     setosa
95           6.7          3.0           5.2          2.3  virginica
96           6.3          2.5           5.0          1.9  virginica
97           6.5          3.0           5.2          2.0  virginica
98           6.2          3.4           5.4          2.3  virginica
99           5.9          3.0           5.1          1.8  virginica

[100 rows x 5 columns]

I am the author of the datar package. Feel free to submit issues if you have any questions.

0

The pandas package has the .str method for columns that are strings and the .str method itself contains the .isin() method which is equivalent to the %in% operator in R. Further, as pointed out by @rhug123 the .isin method can be directly applied on a series. I have made the corresponding change to the code below.

Your R code above can be implemented in python using pandas as follows - assuming that iris is a pandas DataFrame:

iris.species.isin(['setosa', 'virginica'])

You can then filter your DataFrame and only keep the rows with species 'setosa' or 'virginica' as follows:

iris[iris.species.isin(['setosa', 'virginica'])]

2
  • 1
    you would want to drop the .str. Isin can be applied on a series or a df, but is not a str method.
    – rhug123
    Jun 12, 2021 at 15:37
  • Thanks @rhug123 ! Thank you for the your suggestion and great knowledge. Jan 14 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.