# Selecting rows from a data frame using multiple conditions

**URL:** <https://discourse.julialang.org/t/selecting-rows-from-a-data-frame-using-multiple-conditions/73282>\
**Category:** Data\
**Tags:** question, dataframes\
**Created:** [December 17, 2021, 8:59pm UTC](https://discourse.julialang.org/t/selecting-rows-from-a-data-frame-using-multiple-conditions/73282 "2021-12-17T20:59:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [December 17, 2021, 8:59pm UTC](https://discourse.julialang.org/t/selecting-rows-from-a-data-frame-using-multiple-conditions/73282/1 "2021-12-17T20:59:49Z")

</div>

I want to select rows from a data frame using multiple conditions  
This works with only one condition:

```julia
d_f = DataFrame(A=[1, 3, 4, 7, 9], B=["a", "c", "c", "D", "c"])
d_f[(d_f.B .== "c"), :]

```

Adding a second, this doesn’t work:

```julia
d_f[(d_f.A .== 3) & (d_f.B .== "c"), :]
# MethodError: no method matching &(::BitArray{1}, ::BitArray{1})

```

What do I need instead? Prior [answers](https://stackoverflow.com/questions/29421092/select-subset-of-rows-of-dataframe-using-multiple-conditions) to this question appear to be outdated. Thanks.

---

<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:** [December 17, 2021, 9:07pm UTC](https://discourse.julialang.org/t/selecting-rows-from-a-data-frame-using-multiple-conditions/73282/2 "2021-12-17T21:07:22Z")

</div>

You need to broadcast the `&`.

```julia
d_f[(d_f.A .== 3) .& (d_f.B .== "c"), :]

```

If you are using version 1.7, you can actually use the safer `&&`, which is actually control flow, rather than `&`, which is “bitwise OR”.

```julia
julia> d_f[(d_f.A .== 3) .&& (d_f.B .== "c"), :]
1×2 DataFrame
 Row │ A B      
     │ Int64 String 
─────┼───────────────
   1 │ 3 c

```

Finally, if you are using DataFramesMeta.jl, you can do

```julia
julia> using DataFramesMeta

julia> @rsubset(d_f, :A == 3, :B == "c")
1×2 DataFrame
 Row │ A B      
     │ Int64 String 
─────┼───────────────
   1 │ 3 c

```
