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.
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