# How to correct the contents of GroupedDataFrame to update it?

**URL:** https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928
**Category:** New to Julia
**Tags:** question
**Created:** [September 8, 2022, 8:43am UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928 "2022-09-08T08:43:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 8, 2022, 8:43am UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/1 "2022-09-08T08:43:48Z")

</div>

how to correct the contents of groupeddataframe to update it?

```julia
group = [:A, :A, :B, :B]
X = 1:4
Y = 5:8
df = DataFrame(; group, X, Y)
julia> df = DataFrame(; group, X, Y)
4×3 DataFrame
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ A 1 5
   2 │ A 2 6
   3 │ B 3 7
   4 │ B 4 8
julia> gdf = groupby(df, :group)
GroupedDataFrame with 2 groups based on key: group
First Group (2 rows): group = :A
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ A 1 5
   2 │ A 2 6
⋮
Last Group (2 rows): group = :B
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ B 3 7
   2 │ B 4 8

```

How can I change the content of gdf directly? I want to remove the First line of A of gdf’s First Group so that GroupedDataFrame becomes new? It looks like this

```julia
julia> gdf
GroupedDataFrame with 2 groups based on key: group
First Group (1 row): group = :A
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ A 2 6
⋮
Last Group (2 rows): group = :B
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ B 3 7
   2 │ B 4 8

```

Thanks for helping me!

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 8, 2022, 11:14am UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/2 "2022-09-08T11:14:50Z")

</div>

It looks like you’ll have to delete the row in the original dataframe and then **recreate** the grouped df.

```julia
julia> deleteat!(df, findfirst(==(:A), df.group))
3×3 DataFrame
 Row │ group X Y     
     │ Symbol Int64 Int64 
─────┼──────────────────────
   1 │ A 2 6
   2 │ B 3 7
   3 │ B 4 8

julia> gdf = groupby(df, :group)
GroupedDataFrame with 2 groups based on key: group

```

Each group in a `GroupedDataFrame` is a `SubDataFrame`, and those can’t be modified directly.

```julia
julia> deleteat!(gdf[1], 1)
ERROR: ArgumentError: SubDataFrame does not support deleting rows

```

---

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 8, 2022, 12:53pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/3 "2022-09-08T12:53:24Z")

</div>

> [@digital\_carver](#):
>
> `SubDataFrame does not support deleting rows`

Thanks very much

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [September 8, 2022, 1:01pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/4 "2022-09-08T13:01:20Z")

</div>

You could `subset` the `GroupedDataFrame`, but then you’ll have a `DataFrame`:

```julia
subset(gdf, [:group, :X] => ((g, x) -> (g .!= :A) .|| (x .> 1)))
3×3 DataFrame
 Row │ group X Y
     │ Symbol Int64 Int64
─────┼──────────────────────
   1 │ A 2 6
   2 │ B 3 7
   3 │ B 4 8

```

---

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 8, 2022, 1:14pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/6 "2022-09-08T13:14:54Z")

</div>

Sorry,I want to iterate over each Subdataframe, and if I do what you said, it might be difficult.

---

<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 8, 2022, 1:34pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/7 "2022-09-08T13:34:52Z")

</div>

Something like this?

```julia
julia> combine(gdf) do sdf
           sdf[1, :]
       end
2×3 DataFrame
 Row │ group X Y     
     │ Symbol Int64 Int64 
─────┼──────────────────────
   1 │ A 1 5
   2 │ B 3 7

```

EDIT: You can actually do `ungoup = false` to preserve the grouped-ness

```julia
julia> combine(gdf; ungroup = false) do sdf
           sdf[1, :]
       end
GroupedDataFrame with 2 groups based on key: group
First Group (1 row): group = :A
 Row │ group X Y     
     │ Symbol Int64 Int64 
─────┼──────────────────────
   1 │ A 1 5
⋮
Last Group (1 row): group = :B
 Row │ group X Y     
     │ Symbol Int64 Int64 
─────┼──────────────────────
   1 │ B 3 7

```

---

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 8, 2022, 2:02pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/9 "2022-09-08T14:02:41Z")

</div>

Thanks. If I want subset A’s firstline and B’s second line,and return a GroupedDataFrame,what should I do?

---

<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 8, 2022, 2:24pm UTC](https://discourse.julialang.org/t/how-to-correct-the-contents-of-groupeddataframe-to-update-it/86928/10 "2022-09-08T14:24:27Z")

</div>

Hmmmm… probably an `if-else` statement inside the function.
