# Filter doesn't work on grouped dataframe

**URL:** <https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [August 8, 2019, 6:12pm UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322 "2019-08-08T18:12:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 8, 2019, 6:12pm UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/1 "2019-08-08T18:12:27Z")

</div>

The problem is simple, I have a grouped data frame and I want to throw out groups. filter however, does not work. This seems like a very common idiom, so I’m thinking I’m missing some very simple way to handle it.

p.s. i meant within the DataFrame environment. clearly i can create my own filter function and just push!(a, g) to collect the groups i want.

Thanks,

```julia
julia> using DataFrames
x
julia> x=DataFrame([[1,1,2,2,3,3],collect(1:6)],[:a,:b])
6×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 1 │
│ 2 │ 1 │ 2 │
│ 3 │ 2 │ 3 │
│ 4 │ 2 │ 4 │
│ 5 │ 3 │ 5 │
│ 6 │ 3 │ 6 │

julia> g=groupby(x, :a)
GroupedDataFrame with 3 groups based on key: a
First Group (2 rows): a = 1
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 1 │
│ 2 │ 1 │ 2 │
⋮
Last Group (2 rows): a = 3
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 3 │ 5 │
│ 2 │ 3 │ 6 │

julia> filter(y->y[1,:a]==3,g)
ERROR: MethodError: no method matching filter(::getfield(Main, Symbol("##3#4")), ::GroupedDataFrame{DataFrame})
Closest candidates are:
  filter(::Any, ::Array{T,1} where T) at array.jl:2351
  filter(::Any, ::BitArray) at bitarray.jl:1710
  filter(::Any, ::AbstractArray) at array.jl:2312
  ...
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [August 10, 2020, 4:15am UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/2 "2020-08-10T04:15:15Z")

</div>

Encountered the same problem, and hope someone could help.  
From what I found, neither DataFrames.jl nor Query.jl provide a solution to filter grouped data based on the grouping variable. Are there other solutions around? Thanks.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [August 10, 2020, 5:55am UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/3 "2020-08-10T05:55:33Z")

</div>

> [@purplishrock](#):
>
> `filter(y->y[1,:a]==3,g)`

I’ve found `filter(y->y[1,:a]==3,collect(g))` works, but there maybe a better way.

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [August 10, 2020, 6:13pm UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/4 "2020-08-10T18:13:11Z")

</div>

An alternative is to use Query.jl. For example, `df |> @groupby(_.a) |> @filter(key(_) .== 3)`.

---

<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:** [August 10, 2020, 6:33pm UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/5 "2020-08-10T18:33:50Z")

</div>

This will work in the next release of DataFrames.

In the meantime you can use `combine`

```julia
combine(gd) do sdf
    first(sdf.a) == 1 ? DataFrame() : sdf
end

```

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://discourse.julialang.org/u/merlin)\
**Post date:** [February 4, 2022, 5:32am UTC](https://discourse.julialang.org/t/filter-doesnt-work-on-grouped-dataframe/27322/6 "2022-02-04T05:32:44Z")

</div>

Posting this in 2022 because this is where my search led. I got a satisfying result using the docstring from `help?> groupby`.

I’m in DataFrames 1.2 today. You can index into grouped dataframe keys a few ways, for example:

```julia
last(keys(gd))
# GroupKey: (a = 3,)

gd[(a = 3,)]
# 2×5 SubDataFrame
#│ Row │ a │ b │
#│ │ Int64 │ Int64 │
#├─────┼───────┼───────┤
#│ 1 │ 3 │ 5 │
#│ 2 │ 3 │ 6 │

```
