# How to easily rename column of GroupedDataFrame

**URL:** <https://discourse.julialang.org/t/how-to-easily-rename-column-of-groupeddataframe/41505>\
**Category:** General Usage\
**Created:** [June 16, 2020, 10:41am UTC](https://discourse.julialang.org/t/how-to-easily-rename-column-of-groupeddataframe/41505 "2020-06-16T10:41:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssu/32/11306_2.png) [@SSU](https://discourse.julialang.org/u/SSU)\
**Post date:** [June 16, 2020, 10:41am UTC](https://discourse.julialang.org/t/how-to-easily-rename-column-of-groupeddataframe/41505/1 "2020-06-16T10:41:14Z")

</div>

Hi!  
I want to know how to rename the column of GroupedDataFrame easily.

The following code is an example of what I want to do.  
Note: the real data I want to handle is a huge dataframe(~ 10 millions of rows).

Any suggestions are welcomed !!  
Thank you in advance.

```julia
using CategoricalArrays
using CSV
using DataFrames
import MLJBase.int

iris = DataFrame(CSV.File(joinpath(dirname(pathof(DataFrames)), 
            "../docs/src/assets/iris.csv")))
gdf = groupby(iris, :Species)

# bin the data and convert the label(CategoricalArray) to int
# transform! function will name the new column as :SepaiWidth_function automatically.
transform!(
    gdf, :SepalWidth => x -> int(
        cut(x, range(0, stop=10, length=10),
            extend=true), type=Int), ungroup=false
        )
# specifying the column name here didn't work.
# please try this code
# transform!(
# gdf, :SepalWidth => x -> 
# int(cut(x, range(0, stop=10, length=10),extend=true), type=Int) => :new_name,
# ungroup=false
# )

# this does not work
rename!(gdf, :SepalWidth_function => :new_name)

```

---

<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:** [June 16, 2020, 11:41am UTC](https://discourse.julialang.org/t/how-to-easily-rename-column-of-groupeddataframe/41505/2 "2020-06-16T11:41:34Z")

</div>

I think you just need to enclose your anonymous function in brackets if you want to call `:a => (x -> somefun(x)) => :new_name)`

Compare:

```julia
julia> using DataFrames

julia> df = DataFrame(a = rand(1:5, 10), b = rand(10))

julia> combine(groupby(df, :a), :b => (x -> sum(x.^2)) => :c)
4×2 DataFrame
│ Row │ a │ c │
│ │ Int64 │ Float64 │
├─────┼───────┼───────────┤
│ 1 │ 4 │ 1.60384 │
│ 2 │ 3 │ 0.0293582 │
│ 3 │ 2 │ 0.787128 │
│ 4 │ 1 │ 1.24591 │

julia> combine(groupby(df, :a), :b => x -> sum(x.^2) => :c)
4×2 DataFrame
│ Row │ a │ b_function │
│ │ Int64 │ Pair… │
├─────┼───────┼───────────────┤
│ 1 │ 4 │ 1.60384=>:c │
│ 2 │ 3 │ 0.0293582=>:c │
│ 3 │ 2 │ 0.787128=>:c │
│ 4 │ 1 │ 1.24591=>:c │
```

---

<div class="post-metadata">

**Author:** ![SSU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssu/32/11306_2.png) [@SSU](https://discourse.julialang.org/u/SSU)\
**Post date:** [June 16, 2020, 1:58pm UTC](https://discourse.julialang.org/t/how-to-easily-rename-column-of-groupeddataframe/41505/3 "2020-06-16T13:58:49Z")

</div>

This solution was much more simple than I expected…  
Sounds like I need to read documents more carefully.

Thank you so much!!
