# Normalized nrow in a GroupedDataFrame

**URL:** <https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226>\
**Category:** Data\
**Tags:** question, dataframes\
**Created:** [April 29, 2021, 7:51am UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226 "2021-04-29T07:51:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [April 29, 2021, 7:51am UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226/1 "2021-04-29T07:51:57Z")

</div>

What is the best way to do a “normalized” nrow in a gdf?

```julia
using DataFrames
using Chain
df = DataFrame(id=1:6,
                      name=["Aaron Aardvark", "Belen Barboza",
                              "春 陈", "Даниил Дубов",
                              "Elżbieta Elbląg", "Felipe Fittipaldi"],
                      age=[50, 45, 40, 35, 30, 25],
                      eye=["blue", "brown", "hazel", "blue", "green", "brown"],
                      grade_1=[95, 90, 85, 90, 95, 90],
                      grade_2=[75, 80, 65, 90, 75, 95],
                      grade_3=[85, 85, 90, 85, 80, 85])
@chain df begin
    groupby(:eye)
    combine(nrow => :n, x -> nrow(x) / nrow(df))
end

```

That outputs:

```julia
4×3 DataFrame
 Row │ eye n x1       
     │ String Int64 Float64  
─────┼─────────────────────────
   1 │ blue 2 0.333333
   2 │ brown 2 0.333333
   3 │ hazel 1 0.166667
   4 │ green 1 0.166667

```

But it i try to rename the x1 column I get a strange thing:

```julia
 @chain df begin
           groupby(:eye)
           combine(nrow => :n, x -> nrow(x) / nrow(df) => :perc)
       end
4×3 DataFrame
 Row │ eye n x1              
     │ String Int64 Pair…           
─────┼────────────────────────────────
   1 │ blue 2 0.333333=>:perc
   2 │ brown 2 0.333333=>:perc
   3 │ hazel 1 0.166667=>:perc
   4 │ green 1 0.166667=>:perc

```

---

<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:** [April 29, 2021, 7:59am UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226/2 "2021-04-29T07:59:50Z")

</div>

That’s operatory precedence for you:

```julia
julia> @chain df begin
           groupby(:eye)
           combine(nrow => :n, :name => (x -> length(x) / nrow(df)) => :perc)
       end
4×3 DataFrame
 Row │ eye n perc     
     │ String Int64 Float64  
─────┼─────────────────────────
   1 │ blue 2 0.333333
   2 │ brown 2 0.333333
   3 │ hazel 1 0.166667
   4 │ green 1 0.166667

```

(Note the brackets around the anonymous function)

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [April 29, 2021, 8:02am UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226/3 "2021-04-29T08:02:32Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 29, 2021, 8:10am UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226/4 "2021-04-29T08:10:54Z")

</div>

Why does the first case work? I don’t see anything in the docs for passing just a function like that, only `cols => function` or `cols => function => newcols`. And why is that column called `x1`?

```julia
julia> @chain df begin
           groupby(:eye)
           combine(y -> 1)
       end
4×2 DataFrame
 Row │ eye x1    
     │ String Int64 
─────┼───────────────
   1 │ blue 1
   2 │ brown 1
   3 │ hazel 1
   4 │ green 1

julia> @chain df begin
           groupby(:eye)
           combine(y -> 1, x -> 2)
       end
ERROR: ArgumentError: duplicate output column name: :x1

```

---

<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:** [April 29, 2021, 12:22pm UTC](https://discourse.julialang.org/t/normalized-nrow-in-a-groupeddataframe/60226/5 "2021-04-29T12:22:59Z")

</div>

It’s list item 7 [here](https://dataframes.juliadata.org/stable/man/split_apply_combine/). You can pass a function which accepts a `SubDataFrame`. But I guess it doesn’t generate names perfectly so you get an error where it tries to make `:x1` twice.
