Conditional element extraction from data frame: R is% in%, Python is .isin ()

As an R user, make a note of the operations that I often forget when writing python. Even if I searched for "R% in% in python" each time, I couldn't find the information I wanted immediately.

Extract rows where a particular column matches any of multiple elements

Data frame: iris Specific column name: Species Multiple elements: cond (extract only "setosa" or "virginica")

Operations that can be written using the% in% operator in R are

### R ###
library(dplyr)

cond <- c("setosa", "virginica")
df <- iris %>% dplyr::filter(., Species %in% cond)

With Pandas, you can write with .isin ().

### python ###
import pandas as pd
from sklearn import datasets

###iris dataset preparation
iris_sk = datasets.load_iris()
iris = pd.DataFrame(iris_sk.data, columns=iris_sk.feature_names)
iris['Species'] = iris_sk.target_names[iris_sk.target]

cond = ["setosa", "virginica"]
df = iris[iris["Species"].isin(cond)]

If you want to exclude matching lines, use ! And ~ respectively.

### R ###
df2 <- iris %>% dplyr::filter(., !Species %in% cond)
### Python ###
df2 = iris[~iris["Species"].isin(cond)]

Recommended Posts

Conditional element extraction from data frame: R is% in%, Python is .isin ()
Comparison of data frame handling in Python (pandas), R, Pig
Hashing data in R and Python
Get time series data from k-db.com in Python
Receive dictionary data from a Python program in AppleScript
Get data from GPS module at 10Hz in Python
Find the part that is 575 from Wikipedia in Python
Generate Word Cloud from case law data in python3
Hit REST in Python to get data from New Relic
[Python] Random data extraction / combination from DataFrame using random and pandas
Handle Ambient data in Python
OCR from PDF in Python
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
Get Leap Motion data in Python.
Python: Exclude tags from html data
Difference between == and is in python
Read Protocol Buffers data in Python3
Hit treasure data from Python Pandas
Use fabric as is in python (fabric3)
Run shell command / python in R
Python is UnicodeEncodeError in CodeBox docker
Handle NetCDF format data in Python
Extract text from images in Python
There is no switch in python
Python in is also an operator
Rectangle area element split in Python
Extract strings from files in Python
Comparing R, Python, SAS, SPSS from the perspective of European data scientists
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
If you use Pandas' Plot function in Python, it is really seamless from data processing to graph creation