# Create variable name in dataframe that is groupby key

**URL:** https://discourse.julialang.org/t/create-variable-name-in-dataframe-that-is-groupby-key/50590
**Category:** Data
**Created:** [November 22, 2020, 5:46pm UTC](https://discourse.julialang.org/t/create-variable-name-in-dataframe-that-is-groupby-key/50590 "2020-11-22T17:46:44Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)
#### Post date: [November 22, 2020, 5:46pm UTC](https://discourse.julialang.org/t/create-variable-name-in-dataframe-that-is-groupby-key/50590/1 "2020-11-22T17:46:44Z")

</div>

Suppose we have the dataframe:

df = DataFrame(Dict( :name =\> (“AAA”, “AAA”, “AAA”, “AAB”, “AAB”, “CCC”, “DDE”, “DDE”))

Each unique string in :name represents a group. We can use the split-apply-combine strategy described in Dataframes.jl to perform operations on other variables within each group.

I would also like to create a variable in the original dataframe equal to the keys of the groupby result.

There are less direct ways of doing this. For example, I could do

df.grp\_number = (x → Dict(n → i for (n,i) in enumerate(unique(def.name)))).(df.name)

and then do split-apply-combine strategies separately. But this feels inelegant.

Does there exist a syntax like:

combine(grouby(df, :name), key =\> :grp\_number)

?

---

<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: [November 22, 2020, 8:07pm UTC](https://discourse.julialang.org/t/create-variable-name-in-dataframe-that-is-groupby-key/50590/2 "2020-11-22T20:07:31Z")

</div>

First note that `GroupDataFrame` can be reordered and subsetted. Having said that there is no “simple” syntax to get group number, but your approach with `unique` is incorrect because of this.

The way to get group keys is to use `keys` function on `GroupedDataFrame`. It will give you a mapping from grup index to grouping variable value. I am not sure if this would work for you. I could help more if you explained how you want to use the group number later.
