How to easily rename column of GroupedDataFrame

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.

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)

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> 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   β”‚
2 Likes

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

Thank you so much!!