# Dataframe delete duplicate with condition

**URL:** <https://discourse.julialang.org/t/dataframe-delete-duplicate-with-condition/29137>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [September 24, 2019, 11:23pm UTC](https://discourse.julialang.org/t/dataframe-delete-duplicate-with-condition/29137 "2019-09-24T23:23:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Bowles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshua_bowles/32/4515_2.png) [@Joshua\_Bowles](https://discourse.julialang.org/u/Joshua_Bowles)\
**Post date:** [September 24, 2019, 11:23pm UTC](https://discourse.julialang.org/t/dataframe-delete-duplicate-with-condition/29137/1 "2019-09-24T23:23:21Z")

</div>

I’m trying to get from this `df`

```julia
df = DataFrame(A = ["jj","jj","ab","ac","ac"], B = [0, 1, 0, 0, 1], C = ["M", "M", "F", "M", "M"])
#==
5×3 DataFrame
│ Row │ A │ B │ C │
│ │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1 │ jj │ 0 │ M │
│ 2 │ jj │ 1 │ M │
│ 3 │ ab │ 0 │ F │
│ 4 │ ac │ 0 │ M │
│ 5 │ ac │ 1 │ M │
==#

```

to this `df2`

```julia
df2 = DataFrame(A = ["jj","ab","ac"], B = [1, 0, 1], C = ["M", "F", "M"])
#==
3×3 DataFrame
│ Row │ A │ B │ C │
│ │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1 │ jj │ 1 │ M │
│ 2 │ ab │ 0 │ F │
│ 3 │ ac │ 1 │ M │
==#

```

Basically, I need to dedup the dataframe on column A, but on the condition that column B == `0`. I thought I could loop over a grouped data frame and filter but I was not aware you could not delete from a sub dataframe (e.g., this error `#ERROR: ArgumentError: SubDataFrame does not support deleting row`) .

```julia
for g in groupby(df, :A)
    if size(g, 1) > 1
        filter!(row->row[:B] == 1, g)
    end
end

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [September 25, 2019, 1:30am UTC](https://discourse.julialang.org/t/dataframe-delete-duplicate-with-condition/29137/2 "2019-09-25T01:30:12Z")

</div>

```julia
by(df, :A) do sbdf
    (size(sbdf, 1)>1) ? sbdf[sbdf.B.==1,:] : sbdf
end

```

For a large dataframe, there are potentially faster solutions, see [Split-apply-combine · DataFrames.jl](http://juliadata.github.io/DataFrames.jl/stable/man/split_apply_combine/)

---

<div class="post-metadata">

**Author:** ![Joshua\_Bowles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshua_bowles/32/4515_2.png) [@Joshua\_Bowles](https://discourse.julialang.org/u/Joshua_Bowles)\
**Post date:** [September 25, 2019, 1:36am UTC](https://discourse.julialang.org/t/dataframe-delete-duplicate-with-condition/29137/3 "2019-09-25T01:36:00Z")

</div>

nice. thank you
