# Delete Id from dataframe if variable of id contains particular value

**URL:** <https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 10, 2022, 11:29am UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157 "2022-02-10T11:29:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 10, 2022, 11:29am UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/1 "2022-02-10T11:29:26Z")

</div>

hi i want create a new dataframe by deleting some ids from the original data frame on condition.  
data frame i have

```julia
df = DataFrame()
df.id = repeat(1:3,3)
df.v1 = 1:9
df.v2 = 20:28
sort!(df,:id)

```

i want to delete total rows of id , if id have a value of more than 8 in column v1  
data frame i want is

```julia
df2 = DataFrame() 
df2.id = repeat(1:2,3)
df2.v1 = [1,4,7,2,5,8]
df2.v2 = [20,23,26,21,24,27]
sort!(df2,:id)

```

can some body help me to solve this  
Thanks

---

<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:** [February 10, 2022, 12:12pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/2 "2022-02-10T12:12:12Z")

</div>

I’m not sure I understand the question, it seems you are missing a row in `df2` (shouldn’t the row where `v1` equals 3 be included?)

Anyway two ways:

```julia
julia> df[df.v1 .<= 8, :]
8×3 DataFrame
 Row │ id v1 v2    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 1 20
   2 │ 1 4 23
   3 │ 1 7 26
   4 │ 2 2 21
   5 │ 2 5 24
   6 │ 2 8 27
   7 │ 3 3 22
   8 │ 3 6 25

julia> filter!(:v1 => <=(8), df)
8×3 DataFrame
 Row │ id v1 v2    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 1 20
   2 │ 1 4 23
   3 │ 1 7 26
   4 │ 2 2 21
   5 │ 2 5 24
   6 │ 2 8 27
   7 │ 3 3 22
   8 │ 3 6 25

```

the second changes `df` in place.

Have you worked through

[https://github.com/bkamins/Julia-DataFrames-Tutorial](https://github.com/bkamins/Julia-DataFrames-Tutorial)

It seems like the type of questions you are asking are well covered by this, e.g. all the row subsetting stuff is here:

[https://github.com/bkamins/Julia-DataFrames-Tutorial/blob/master/06\_rows.ipynb](https://github.com/bkamins/Julia-DataFrames-Tutorial/blob/master/06_rows.ipynb)

(which also discusses `subset` as an additional option)

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 10, 2022, 12:42pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/3 "2022-02-10T12:42:56Z")

</div>

hi @nilshg , my id 3 has one value in v1 which is more than 8, so i want to deleted whole id 3 from the dataset

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 10, 2022, 12:43pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/4 "2022-02-10T12:43:39Z")

</div>

Thanks , i will go through this

---

<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:** [February 10, 2022, 1:50pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/5 "2022-02-10T13:50:03Z")

</div>

Ah that’s the bit I didn’t understand, you want something like

```julia
julia> df[transform(groupby(df, :id), :v1 => maximum).v1_maximum .< 9, :]
6×2 DataFrame
 Row │ id v1    
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 1
   2 │ 2 2
   3 │ 1 4
   4 │ 2 5
   5 │ 1 7
   6 │ 2 8

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 10, 2022, 3:13pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/6 "2022-02-10T15:13:19Z")

</div>

FWIW, another one-liner option:

```julia
df[df.id .∉ (df.id[findall(>(8), df.v1)],), :]

```

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sai_matcha/32/35801_2.png) [@sai\_matcha](https://discourse.julialang.org/u/sai_matcha)\
**Post date:** [February 10, 2022, 4:11pm UTC](https://discourse.julialang.org/t/delete-id-from-dataframe-if-variable-of-id-contains-particular-value/76157/7 "2022-02-10T16:11:04Z")

</div>

@rafael.guerra Thank you
