pwwang / datar

A Grammar of Data Manipulation in python
https://pwwang.github.io/datar/
MIT License
272 stars 17 forks source link

how to use %in% #207

Closed fkgruber closed 8 months ago

fkgruber commented 8 months ago

Question about datar

In datar how to filter on a vector of possible values. For example, in R I would do:

library(tidyverse)
tst = iris %>% filter(Species %in% c("setosa", "versicolor"))
tst$Species %>% unique()
[1] setosa     versicolor
Levels: setosa versicolor virginica

If I try something like this in datar using in instead of %in% I get:

from datar.all import filter_, f,pull
from datar.data import iris

tst=iris >> filter_(f.Species in ["setosa","versicolor"])
print(tst.Species.unique())
['setosa' 'versicolor' 'virginica']

Is there something similar to %in% in datar?

pwwang commented 8 months ago

%in% is nothing but just the is.element() function in R. So with datar, we have is_element(). You can find an example on this page: https://pwwang.github.io/datar/notebooks/base/

from datar.all import filter_, f,pull, is_element
from datar.data import iris

tst=iris >> filter_(is_element(f.Species, ["setosa","versicolor"]))
print(tst.Species.unique())

is_in is an alias of is_element.