# How to identify clusters of values in a dataframe (or grouped dataframe)

**URL:** <https://discourse.julialang.org/t/how-to-identify-clusters-of-values-in-a-dataframe-or-grouped-dataframe/127557>\
**Category:** Data\
**Tags:** question\
**Created:** [March 31, 2025, 3:57pm UTC](https://discourse.julialang.org/t/how-to-identify-clusters-of-values-in-a-dataframe-or-grouped-dataframe/127557 "2025-03-31T15:57:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rj3838](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rj3838/32/25844_2.png) [@rj3838](https://discourse.julialang.org/u/rj3838)\
**Post date:** [March 31, 2025, 3:57pm UTC](https://discourse.julialang.org/t/how-to-identify-clusters-of-values-in-a-dataframe-or-grouped-dataframe/127557/1 "2025-03-31T15:57:13Z")

</div>

I am trying to identify clusters of values within a dataframe (it’s actually grouped but I’m handling it in sections). The data is of road defects so the actual data is large, I know that isn’t relevant for this but it gives some idea of the volume involved.

```julia
#Create DataFrame with multiple ranges of the number

data = Dict(
    "A" => [1, 7, 3, 4, 7, 7],
    "B" => [5, 6, 1, 8, 9, 9],
    "C" => [7, 10, 10, 11, 7, 7],
    "D" => [13, 13, 14, 15, 7, 7],
    "E" => [7, 10, 10, 11, 7, 7],
)
df = DataFrame(data)'

```

The values I need to identify in this case are the clusters of the digit 7. That is col A, row 5 and 6 also cols C,D and E in rows 5 and 6.

> 6×5 DataFrame  
> Row │ A B C D E  
> │ Int64 Int64 Int64 Int64 Int64  
> ─────┼─────────────────  
> 1 │ 1 5 7 13 7  
> 2 │ 7 6 10 13 10  
> 3 │ 3 1 10 14 10  
> 4 │ 4 8 11 15 11  
> 5 │ 7 9 7 7 7  
> 6 │ 7 9 7 7 7

The value to find for the cluster will change and there may be any number of clusters.

I can identify where the number 7 is by using enumerate along the rows and cols but I’m trying to find something easier as the result gives the rows and columns without the clusters they belong to.

The output/return value should be a cluster number and it’s position as I need to calculate the max width and height of the cluster.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 31, 2025, 4:43pm UTC](https://discourse.julialang.org/t/how-to-identify-clusters-of-values-in-a-dataframe-or-grouped-dataframe/127557/2 "2025-03-31T16:43:09Z")

</div>

Can you elaborate on why Clustering.jl algorithms can’t be used directly? Or [geostatistical clustering](https://juliaearth.github.io/GeoStatsDocs/stable/clustering) algorithms?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 3, 2025, 8:31pm UTC](https://discourse.julialang.org/t/how-to-identify-clusters-of-values-in-a-dataframe-or-grouped-dataframe/127557/3 "2025-04-03T20:31:52Z")

</div>

> [@rj3838](#):
>
> 6×5 DataFrame  
> Row │ A B C D E  
> │ Int64 Int64 Int64 Int64 Int64  
> ─────┼─────────────────  
> 1 │ 1 5 7 13 7  
> 2 │ 7 6 10 13 10  
> 3 │ 3 1 10 14 10  
> 4 │ 4 8 11 15 11  
> 5 │ 7 9 7 7 7  
> 6 │ 7 9 7 7 7

What outcome would you expect in this case?
