# How to use \`in\` with @where \[DataFramesMeta\]?

**URL:** <https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176>\
**Category:** Data\
**Tags:** question\
**Created:** [February 19, 2017, 6:26pm UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176 "2017-02-19T18:26:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 19, 2017, 6:26pm UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/1 "2017-02-19T18:26:56Z")

</div>

As from the title, how to use `in` with `@where` ([DataFramesMeta](https://github.com/JuliaStats/DataFramesMeta.jl))?

e.g. `@where(df, :x in ["cat1", "cat2"])`

I did already try `:x .in ["cat1", "cat2"]` or `.in(:x, ["cat1", "cat2"])`…

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 19, 2017, 7:55pm UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/2 "2017-02-19T19:55:55Z")

</div>

You actually need your function to take a vector and output a vector of booleans, as `@where` works with functions taking vectors as inputs. For example:

```julia
df = DataFrame(x = 1:3, y = [2, 1, 2])
@where(df, :x .> 1)

```

One easy solution is to use a version of `in` where the first argument can be a vector:

`@where(df, [x in [1,2] for x in :x])`

which of course gets annoying to write so I guess you could define some auxiliary function if you have to do this very often. For example you could define a customized operator `\smallin` :

```julia
as ∊ b = [a in b for a in as]
@where(df, :x ∊ [1,2])

```

On the other hand I’d be curious to know what is the recommended way to select data in a DataFrame. I’m not sure whether it is the `@where` macro from DataFramesMeta or if instead the Query.jl package should be preferred (or some other option that I’m not aware of).

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 19, 2017, 8:29pm UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/3 "2017-02-19T20:29:04Z")

</div>

FWIW, there’s an issue about it [here](https://github.com/JuliaStats/DataFramesMeta.jl/issues/39).

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [February 20, 2017, 7:41pm UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/4 "2017-02-20T19:41:11Z")

</div>

You can use [Query](https://github.com/davidanthoff/Query.jl) for this:

```julia
@from i in df begin
    @where i.x in ["cat1", "cat2"]
    @select i
    @collect DataFrame
end

```

I’m still working on a short-version API for this kind of scenario (the above code really is quite verbose if all you want to do is filter…), but nothing really working at this point.

---

<div class="post-metadata">

**Author:** ![joshbode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshbode/32/260_2.png) [@joshbode](https://discourse.julialang.org/u/joshbode)\
**Post date:** [February 21, 2017, 12:09am UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/5 "2017-02-21T00:09:34Z")

</div>

I use `indexin` for this kind of query, i.e.

```julia
julia> d = DataFrame(x=[1, 2, 3], y="X")
julia> @where(d, indexin(:x, [1, 3]) .> 0)
2×2 DataFrames.DataFrame
│ Row │ x │ y │
├-----┼---┼---┤
│ 1 │ 1 │ X │
│ 2 │ 3 │ X │

```

As a bonus, it is tolerant of `NA` values, too 🙂

---

<div class="post-metadata">

**Author:** ![ValdarT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valdart/32/24146_2.png) [@ValdarT](https://discourse.julialang.org/u/ValdarT)\
**Post date:** [March 30, 2018, 10:24am UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/6 "2018-03-30T10:24:02Z")

</div>

```julia
@where(df, in.(:x, [["cat1", "cat2"]]))

```

Notice that `["cat1", "cat2"]` is wrapped in another `Array` (the iterable is the only element of another iterable).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [December 3, 2020, 1:36am UTC](https://discourse.julialang.org/t/how-to-use-in-with-where-dataframesmeta/2176/7 "2020-12-03T01:36:07Z")

</div>

> [@ValdarT](#):
>
> `@where(df, in.(:x, [["cat1", "cat2"]]))`

I really like this thank you.  
Can also be written

`@where(df, :x .∈ [["cat1", "cat2"]])`
