# Note on eachindex behavior with GroupedDataFrame

**URL:** <https://discourse.julialang.org/t/note-on-eachindex-behavior-with-groupeddataframe/124439>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [January 5, 2025, 5:16am UTC](https://discourse.julialang.org/t/note-on-eachindex-behavior-with-groupeddataframe/124439 "2025-01-05T05:16:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [January 5, 2025, 5:16am UTC](https://discourse.julialang.org/t/note-on-eachindex-behavior-with-groupeddataframe/124439/1 "2025-01-05T05:16:13Z")

</div>

`eachindex` used on a GroupedDataFrame, returns a GroupKey, such as `GroupKey: (site = 'b',)`.

When using the indices to create figure layouts in Makie or other plotting packages, (`Axis(fig[i, 1]`), one should explicitly ask for IndexLinear from `eachindex`.

> **demo code**
>
> ```julia
> using DataFrames
> 
> """
> gdfindices()
> 
> Demonstrate behavior of `eachindex` with GroupedDataFrame.  
> """
> function gdfindices()
> df = DataFrame(site = rand('a':'e', 1000), yield = rand(1:100, 1000))
> grpdf = groupby(df, :site)       
> println(" Keys:")
> show(stdout, "text/plain", keys(grpdf))
> 
> naiveindx = eachindex(grpdf)
> println("\n\n eachindex(grpdf):")
> show(stdout, "text/plain", naiveindx)
> 
> indx = eachindex(IndexLinear(), grpdf)
> println("\n\n eachindex, IndexLinear:")
> show(stdout, "text/plain", indx)
> end
> 
> julia> gdfindices()
> Keys:
> 5-element DataFrames.GroupKeys{DataFrames.GroupedDataFrame{DataFrames.DataFrame}}:
> GroupKey: (site = 'b',)
> GroupKey: (site = 'c',)
> GroupKey: (site = 'd',)
> GroupKey: (site = 'a',)
> GroupKey: (site = 'e',)
> 
> eachindex(grpdf):
> 5-element DataFrames.GroupKeys{DataFrames.GroupedDataFrame{DataFrames.DataFrame}}:
> GroupKey: (site = 'b',)
> GroupKey: (site = 'c',)
> GroupKey: (site = 'd',)
> GroupKey: (site = 'a',)
> GroupKey: (site = 'e',)
> 
> eachindex, IndexLinear:
> Base.OneTo(5)
> 
> ```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 5, 2025, 10:56am UTC](https://discourse.julialang.org/t/note-on-eachindex-behavior-with-groupeddataframe/124439/2 "2025-01-05T10:56:04Z")

</div>

In plotting I tend to iterate over `enumerate(pairs(gdf))`. I use the running index to get the axis location with `fldmod1(i, ncols)` and use the keys to set titles or colors etc per group.
