# Dataframes: How to conditionally remove rows based on data values?

**URL:** https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538
**Category:** General Usage
**Created:** [September 30, 2020, 1:33pm UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538 "2020-09-30T13:33:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [September 30, 2020, 1:33pm UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538/1 "2020-09-30T13:33:55Z")

</div>

Dear folks,  
I fail to remove rows in a dataframe based on conditions. Here is an MWE:

The dataframe is

```julia
│ Row │ A │ B │
│ │ String │ String │
├─────┼────────┼────────┤
│ 1 │ X │ C │
│ 2 │ X │ C │
│ 3 │ Y │ C │
│ 4 │ Y │ D │

```

and I want to delete the last row with a logical AND condition. I tried

```julia
using DataFrames, Query, CSV

df = DataFrame(A = ["X", "X", "Y", "Y"], B = ["C", "C", "C", "D"])
df = df |> @filter((_.A != "Y") .& (_.B != "D") ) |> DataFrame
print(df)

```

which, however, removes all rows in which “Y” appears ☹ , i.e.,

```julia
│ Row │ A │ B │
│ │ String │ String │
├─────┼────────┼────────┤
│ 1 │ X │ C │
│ 2 │ X │ C │

```

Thank you in advance for some inputs.

---

<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: [September 30, 2020, 1:37pm UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538/2 "2020-09-30T13:37:06Z")

</div>

Not sure about Query but note that you can always subset easily in plain DataFrames:

```julia
df[(df.A .!= "Y") .& (df.B .!= "D"), :]

```

is pretty readable imho.

You might be using `filter` the wrong way around, cf this thread: [The filter function is non-intuitive - #10 by ianfiske](https://discourse.julialang.org/t/the-filter-function-is-non-intuitive/45674/10) (although the thread is about the `filter` function, not the Query macro)

---

<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: [September 30, 2020, 1:44pm UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538/3 "2020-09-30T13:44:02Z")

</div>

DataFrames has a `filter` function build in. But note that as mentioned above, `filter` _keeps_ rows, rather than removes them.

```julia
julia> df = DataFrame(A = ["X", "X", "Y", "Y"], B = ["C", "C", "C", "D"])
4×2 DataFrame
│ Row │ A │ B │
│ │ String │ String │
├─────┼────────┼────────┤
│ 1 │ X │ C │
│ 2 │ X │ C │
│ 3 │ Y │ C │
│ 4 │ Y │ D │

julia> filter(row -> !(row.A == "Y" && row.B == "D"), df)
3×2 DataFrame
│ Row │ A │ B │
│ │ String │ String │
├─────┼────────┼────────┤
│ 1 │ X │ C │
│ 2 │ X │ C │
│ 3 │ Y │ C │

```

---

<div class="post-metadata">

### Author: ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)
#### Post date: [September 30, 2020, 1:50pm UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538/4 "2020-09-30T13:50:15Z")

</div>

Great, many thanks to both of you 🙂!

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 1, 2020, 1:52am UTC](https://discourse.julialang.org/t/dataframes-how-to-conditionally-remove-rows-based-on-data-values/47538/5 "2020-10-01T01:52:06Z")

</div>

Raise an issue

[https://github.com/JuliaData/DataFrames.jl/issues/2460](https://github.com/JuliaData/DataFrames.jl/issues/2460)
