# Subsetting dataframe with multiple conditions

**URL:** <https://discourse.julialang.org/t/subsetting-dataframe-with-multiple-conditions/75160>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [January 25, 2022, 9:35am UTC](https://discourse.julialang.org/t/subsetting-dataframe-with-multiple-conditions/75160 "2022-01-25T09:35:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobshi/32/19276_2.png) [@lobshi](https://discourse.julialang.org/u/lobshi)\
**Post date:** [January 25, 2022, 9:35am UTC](https://discourse.julialang.org/t/subsetting-dataframe-with-multiple-conditions/75160/1 "2022-01-25T09:35:59Z")

</div>

Why should i wrap every condition in a pair of parentheses when subsetting data? Example dataframe as below:

```julia
d = DataFrame(a = [1, 2, 3], b = ["x", "y", "z"])
d2 = d[(d.a .> 1) .| (d.b == "z"), :]

```

If I remove parentheses, error pops out.

```julia
d2 = d[d.a .> 1 .| d.b == "z", :]
# ERROR: LoadError: MethodError: no method matching |(::Int64, ::String)

```

But no error if i write conditions this way:

```julia
1 > 2 | 2 > 3
# false

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 25, 2022, 9:55am UTC](https://discourse.julialang.org/t/subsetting-dataframe-with-multiple-conditions/75160/2 "2022-01-25T09:55:51Z")

</div>

This was answered on SO here:

[https://stackoverflow.com/questions/70845261/julia-subsetting-dataframe-with-multiple-conditions](https://stackoverflow.com/questions/70845261/julia-subsetting-dataframe-with-multiple-conditions)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 25, 2022, 9:59am UTC](https://discourse.julialang.org/t/subsetting-dataframe-with-multiple-conditions/75160/3 "2022-01-25T09:59:19Z")

</div>

Just to because your example here is a bit different than on SO: when you say

> no error if I write conditions this way  
> `1 > 2 | 2 > 3`

you’re not getting an error, but this probably doesn’t do what you think it does:

```julia
julia> 1 > 2 | 3 > 2
false

```

so still you need either higher precedence `||` or parentheses.

My personal preference is to always disambiguate with parentheses, which is especially beneficial in a language like Julia with a large number of unicode infix operators which make it basically impossible to remember the precedence hierarchy.
