# Boolean Indexing in dataframes

**URL:** https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692
**Category:** Data
**Tags:** question, dataframes
**Created:** [May 31, 2023, 8:53pm UTC](https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692 "2023-05-31T20:53:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ordens\_Ritter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ordens_ritter/32/50363_2.png) [@Ordens\_Ritter](https://discourse.julialang.org/u/Ordens_Ritter)
#### Post date: [May 31, 2023, 8:53pm UTC](https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692/1 "2023-05-31T20:53:03Z")

</div>

> [@Indexing a DataFrame with a boolean DataFrame](https://discourse.julialang.org/t/indexing-a-dataframe-with-a-boolean-dataframe/83108/1):
>
> et some values across a whole datafame using a boolean mask, similarly to this example with plain arrays:

@bkamins How about sampling the dataframe by boolean values. I mean take every column for the row, the the boolean is true?

Like this example:

```julia
julia> df = DataFrame(rand(3,3), :auto)
3×3 DataFrame
 Row │ x1 x2 x3       
     │ Float64 Float64 Float64  
─────┼───────────────────────────────
   1 │ 0.045519 0.468771 0.387336
   2 │ 0.0133922 0.383619 0.418809
   3 │ 0.870746 0.898979 0.628106

```

For example: I want to get every dataframe, at every row where x1 is larger 0.02

---

<div class="post-metadata">

### Author: ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)
#### Post date: [May 31, 2023, 8:58pm UTC](https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692/2 "2023-05-31T20:58:47Z")

</div>

I’m not sure I follow the original question, but for the example you can do

```julia
df[df.x1 .> 0.02, :]

```

to select the subset of rows satisfying the desired criterion.

Alternatively, you can use the subset function:

```julia
subset(df, :x1 => ByRow(>(0.02)))

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [May 31, 2023, 9:19pm UTC](https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692/3 "2023-05-31T21:19:01Z")

</div>

In DataFramesMeta.jl, this is simply

```julia
@rsubset df :x1 > .02

```

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [December 14, 2023, 2:17pm UTC](https://discourse.julialang.org/t/boolean-indexing-in-dataframes/99692/4 "2023-12-14T14:17:45Z")

</div>

I think maybe you want this:

```julia-auto
using Tidier,DataFrames
df = DataFrame(a = [1, 2, missing, 4, 5])

```

## @filter

```julia-auto
@chain df begin
   @filter(a >=2))
end

```

## Conditionals

```julia-auto
@chain df begin
  @mutate(a = if_else(a >= 2, true, false))
end

```
