# Filter rows after a perticular value in column

**URL:** <https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [March 1, 2022, 6:42am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225 "2022-03-01T06:42:32Z")\
**Posts on this page:** 6\
**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:** [March 1, 2022, 6:42am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/1 "2022-03-01T06:42:32Z")

</div>

Hi , I have a dafataframe that looks like

```julia
df = DataFrame()
df.id = sort!(repeat(1:3,5))
df.a = repeat([0,5,7,15,17],3)
df.diff_a = repeat([0,5,2,8,2],3)

```

I want to drop all coming rows whenever ‘df.diff\_a’ value goes more than 6 .  
Data Frame i want is

```julia
df = DataFrame()
df.id = sort!(repeat(1:3,3))
df.a = repeat([0,5,7],3)
df.diff_a = repeat([0,5,2],3)

```

I have dropped all the next rows whenever diff\_a crossed value 6.

Hope i am clear. Thanks

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 1, 2022, 7:35am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/2 "2022-03-01T07:35:22Z")

</div>

Could you please clarify. In the requested output you are also dropping rows when `:diff_a` is less or equal than `6`.

If you want to drop rows greater than 6 do:

```julia
subset(df, :diff_a => ByRow(<=(6)))

```

or using DataFramesMeta.jl (which should be probably easier for you to use):

```julia
@rsubset(df, :diff_a <= 6)

```

---

<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:** [March 1, 2022, 9:28am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/3 "2022-03-01T09:28:34Z")

</div>

There are 5 rows in my each id . id1 contains [0,5,2,8,2] in the df.diff\_a. if any value occurs above 6 in any row of df.diff\_a , i want to delete all the rows of that ID after that value.  
Here value 8 is occuring at 4th row. so i want to delete all rows after 3rd row .

in simple words, if any number greater than 6 occurs in any rows, that row and rows after that row should be filtered or dropped. even if next rows are less than 6.

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:** [March 1, 2022, 9:59am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/4 "2022-03-01T09:59:49Z")

</div>

I would probably do it this way which is quite transparent imho, although there might be more efficient solutions:

First work out which row has the first value \>6 by id, as well as the observation number of each row within id:

```julia
julia> transform!(groupby(df, :id), :diff_a => (x -> findfirst(>(6), x)) => :cutoff, :id => (x -> 1:length(x)) => :n_obs)
15×5 DataFrame
 Row │ id a diff_a cutoff n_obs 
     │ Int64 Int64 Int64 Int64 Int64 
─────┼─────────────────────────────────────
   1 │ 1 0 0 4 1
   2 │ 1 5 5 4 2
   3 │ 1 7 2 4 3
   4 │ 1 15 8 4 4
   5 │ 1 17 2 4 5
   6 │ 2 0 0 4 1
   7 │ 2 5 5 4 2
   8 │ 2 7 2 4 3
   9 │ 2 15 8 4 4
  10 │ 2 17 2 4 5
  11 │ 3 0 0 4 1
  12 │ 3 5 5 4 2
  13 │ 3 7 2 4 3
  14 │ 3 15 8 4 4
  15 │ 3 17 2 4 5

```

Now drop rows where the observation number is equal to or larger than the number of the first \>6 observation:

```julia
julia> df[df.n_obs .< df.cutoff, :]
9×5 DataFrame
 Row │ id a diff_a cutoff n_obs 
     │ Int64 Int64 Int64 Int64 Int64 
─────┼─────────────────────────────────────
   1 │ 1 0 0 4 1
   2 │ 1 5 5 4 2
   3 │ 1 7 2 4 3
   4 │ 2 0 0 4 1
   5 │ 2 5 5 4 2
   6 │ 2 7 2 4 3
   7 │ 3 0 0 4 1
   8 │ 3 5 5 4 2
   9 │ 3 7 2 4 3

```

You might have to adapt this for groups where there is no value larger than 6, in which case `findfirst` will return `nothing`.

---

<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:** [March 1, 2022, 10:21am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/5 "2022-03-01T10:21:34Z")

</div>

The code helped me to get solution. But im not completely understood how it is working. Thank you very much .

---

<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:** [April 26, 2022, 2:55am UTC](https://discourse.julialang.org/t/filter-rows-after-a-perticular-value-in-column/77225/6 "2022-04-26T02:55:58Z")

</div>

Hi, How to handle when returns nothing ?
