# Group by value in column

**URL:** https://discourse.julialang.org/t/group-by-value-in-column/93473
**Category:** General Usage
**Tags:** question, package, dataframes
**Created:** [January 24, 2023, 4:54pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473 "2023-01-24T16:54:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)
#### Post date: [January 24, 2023, 4:54pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/1 "2023-01-24T16:54:21Z")

</div>

How do I get 3 groups here based on the column value? In this case when you find a word all in capitals.

```plaintext
df = DataFrame(a = ["HOLA", 2,3,"HELLO", 4,5,6, "HALLO", 7,8,9,10], b = rand(12), c=rand(12))

```

I want to get 3 groups here. The first one

```julia
Row │ a b c         
     │ Any Float64 Float64   
─────┼─────────────────────────────
   1 │ HOLA 0.189917 0.300812
   2 │ 2 0.579802 0.464355
   3 │ 3 0.306022 0.236678

```

second:

```julia
4 │ HELLO 0.40227 0.931759
   5 │ 4 0.632268 0.673693
   6 │ 5 0.465315 0.0911626
   7 │ 6 0.0710601 0.231631

```

and third:

```julia
8 │ HALLO 0.199382 0.851106
   9 │ 7 0.114058 0.0537212
  10 │ 8 0.988661 0.842176
  11 │ 9 0.0238035 0.0757923
  12 │ 10 0.672239 0.335123

```

thanks for any tips. I don’t want to this by hand for hundreds of column values.

---

<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: [January 24, 2023, 4:59pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/2 "2023-01-24T16:59:59Z")

</div>

I would like to do it like this

```julia
julia> @chain df begin
           @rtransform :all_caps = :a isa AbstractString && all(isuppercase, :a)  
           @transform :num_all_caps = cumsum(:all_caps)
           groupby(:num_all_caps)
       end
GroupedDataFrame with 3 groups based on key: num_all_caps
First Group (3 rows): num_all_caps = 1
 Row │ a b c all_caps num_all_caps 
     │ Any Float64 Float64 Bool Int64        
─────┼───────────────────────────────────────────────────
   1 │ HOLA 0.808749 0.38041 true 1
   2 │ 2 0.783565 0.359544 false 1
   3 │ 3 0.0322656 0.628813 false 1
⋮
Last Group (5 rows): num_all_caps = 3
 Row │ a b c all_caps num_all_caps 
     │ Any Float64 Float64 Bool Int64        
─────┼───────────────────────────────────────────────────
   1 │ HALLO 0.39854 0.770258 true 3
   2 │ 7 0.448122 0.270023 false 3
   3 │ 8 0.156094 0.509108 false 3
   4 │ 9 0.802457 0.805941 false 3
   5 │ 10 0.898761 0.758413 false 3

```

---

<div class="post-metadata">

### Author: ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)
#### Post date: [January 24, 2023, 5:23pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/3 "2023-01-24T17:23:30Z")

</div>

Thanks, although it looks overcomplicated. But it seems to do the job. Well, on second thought, maybe this is the shortest way.

---

<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: [January 24, 2023, 5:35pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/4 "2023-01-24T17:35:30Z")

</div>

There’s no `cumsum(f::Function, x)` method, so I think this is basically the shortest way (though you can always avoid making two `@transform` statements)

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [January 24, 2023, 5:59pm UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/5 "2023-01-24T17:59:02Z")

</div>

There is no `cumsum(f::Function, x)` but there is `cumsum(itr)`.

```julia
@chain df begin
    transform(:a => (z -> cumsum(x isa AbstractString && all(isuppercase, x) for x in z)) => :num_all_caps)
    groupby(:num_all_caps)
end

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 25, 2023, 1:18am UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/6 "2023-01-25T01:18:41Z")

</div>

Another option:

```julia
ix = [findall(x -> (isa(x, String) && all(isuppercase, x)), df.a); nrow(df)+1]

[df[ix[i]:ix[i+1]-1,:] for i in 1:length(ix)-1]

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [January 25, 2023, 2:14am UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/7 "2023-01-25T02:14:52Z")

</div>

Another option:

```julia
import Base.Iterators as Itr

df.g .= Itr.map(let g = "" ; x -> ( x isa AbstractString && all(isuppercase, x) ? (g = x) : g ) ; end, df.a)

# optional:
# using CategoricalArrays
# df.g = categorical(df.g)

DataFrames.groupby(df, :g)

```

---

<div class="post-metadata">

### Author: ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)
#### Post date: [January 25, 2023, 8:42am UTC](https://discourse.julialang.org/t/group-by-value-in-column/93473/8 "2023-01-25T08:42:55Z")

</div>

thanks everyone for your feedback. As expected there are wild ways to do it. Good to have them now here for other users 😄
