# Filter DataFrame by an Array

**URL:** <https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328>\
**Category:** New to Julia\
**Created:** [February 26, 2018, 2:32am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328 "2018-02-26T02:32:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max-Zara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max-zara/32/3393_2.png) [@Max-Zara](https://discourse.julialang.org/u/Max-Zara)\
**Post date:** [February 26, 2018, 2:32am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/1 "2018-02-26T02:32:50Z")

</div>

Hi,  
I want to sub-select based on specific values from a Dataframe, like in R would be done by  
valuesX %in% listY

temp\_df = DataFrame(IndexVal = 1:10, Names = [“A”,“B”,“C”,“A1”,“B1”,“C1”,“A2”,“B2”,“C2”,“A3”])

temp\_df[[temp\_df[:IndexVal] in [1,3,5]],:]

Here I want to select only the values where IndexVal is 1,3 or 5.

How do I do this?

Thank you in advance

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [February 26, 2018, 3:19am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/2 "2018-02-26T03:19:04Z")

</div>

Sounds like you need [my favorite function that’s not in base](https://stackoverflow.com/questions/29661315/vectorized-in-function-in-julia).

```julia
function vectorin(a, b)
    bset = Set(b)
    [i in bset for i in a]
end
temp_df[vectorin(temp_df[:IndexVal], [1,3,5]),:]

```

---

<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:** [February 26, 2018, 4:39am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/3 "2018-02-26T04:39:02Z")

</div>

I think a more idiomatic way of doing this in Julia would be to use `filter`:

```julia
filter(row -> row[:IndexVal] in [1,3,5], temp_df)

```

But in order to get `in` working as you want, you need to use `.` to vectorize it and use a little “trick”:

```julia
temp_df[in.(temp_df[:IndexVal], ([1,3,5],)), :]

```

The trick there is to have the second array as an (only) element of some other iterable (I used a 1-element tuple but it can also be an array of arrays - `[[1,3,5]]`).  
You can read more about vectorizing/broadcasting from the [Functions section of the manual](https://docs.julialang.org/en/release-0.6/manual/functions/#man-vectorized-1).

Yet another way is to use `findin` to get the desired indices:

```julia
temp_df[findin(temp_df[:IndexVal], [1,3,5]), :]

```

---

<div class="post-metadata">

**Author:** ![Max-Zara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max-zara/32/3393_2.png) [@Max-Zara](https://discourse.julialang.org/u/Max-Zara)\
**Post date:** [February 26, 2018, 12:53pm UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/4 "2018-02-26T12:53:47Z")

</div>

Thank you both - very clear and concise

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stu/32/8772_2.png) [@stu](https://discourse.julialang.org/u/stu)\
**Post date:** [June 12, 2019, 1:47pm UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/5 "2019-06-12T13:47:08Z")

</div>

> [@ValdarT](#):
>
> findin

is findin still supported currently?

---

<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:** [June 15, 2019, 6:21am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/6 "2019-06-15T06:21:25Z")

</div>

It is not. You can use `findall(in(A), B)` instead:

```julia
temp_df[findall(in([1,3,5]), temp_df[:IndexVal]), :]

```

---

<div class="post-metadata">

**Author:** ![Topher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topher/32/13701_2.png) [@Topher](https://discourse.julialang.org/u/Topher)\
**Post date:** [December 10, 2019, 2:30am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/7 "2019-12-10T02:30:23Z")

</div>

@ValdarT could you also post the syntax needed to select values from temp\_df where IndexVal is NOT 1, 3, or 5? My first impression is to surround in([1,3,5]) with Not() but that can’t be right.

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [December 10, 2019, 6:02am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/8 "2019-12-10T06:02:23Z")

</div>

`Not` is meant to be used for selection of columns, what you are looking for is just the classical predicate negation function: `!`

```julia
temp_df[findall(!in([1,3,5]), temp_df[!,:IndexVal]), :]

```

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 10, 2019, 6:21am UTC](https://discourse.julialang.org/t/filter-dataframe-by-an-array/9328/9 "2019-12-10T06:21:33Z")

</div>

You may want to check out the latest doc: [Getting Started · DataFrames.jl](http://juliadata.github.io/DataFrames.jl/v0.20/man/getting_started/)

So for your case, the standard way should be:  
`temp_df[in.(temp_df.IndexVal, Ref([1,3,5]), :]`  
or  
`temp_df[in([1,3,5]).(temp_df.IndexVal), :]`

`Ref` here is used to protect parameter that you don’t want broadcast operation to apply to ([1,3,5] in your example)
