# How to count the number of categories present in a column of a DataFrame

**URL:** <https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244>\
**Category:** Data\
**Tags:** question, dataframes\
**Created:** [January 11, 2020, 5:24pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244 "2020-01-11T17:24:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![philc](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@philc](https://discourse.julialang.org/u/philc)\
**Post date:** [January 11, 2020, 5:24pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/1 "2020-01-11T17:24:54Z")

</div>

Hello,

In a dataframe with columns :A, :B, :C where :B contains categorical data, I would like to count the number of categories present in the different subdataframes obtained through groupby(df,:A), and finally getting the list of codes in column :A where more than one category is present.

Thank you very much for any advice,  
Philippe

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [January 11, 2020, 6:16pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/2 "2020-01-11T18:16:06Z")

</div>

Welcome! Can you post a sample dataframe? It’ll be easier to help you then.

---

<div class="post-metadata">

**Author:** ![philc](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@philc](https://discourse.julialang.org/u/philc)\
**Post date:** [January 11, 2020, 7:38pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/3 "2020-01-11T19:38:51Z")

</div>

Here is an example:  
Starting with the following data  
df = DataFrame(A = [“a”,“a”,“a”,“b”,“b”,“b”,“c”,“c”],  
B = [“X”,“Y”,“Z”,“Y”,“Y”,“Z”,“X”,“X”],  
C = [2,3,5,2,10,7,5,1])  
I would like to count for each value of the A column the number of different values in column B, the result would be something like:  
a 3  
b 2  
c 1  
could be tuples, arrays, a dictionary…

Hope this is clearer!  
Thanks for your attention.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [January 11, 2020, 7:55pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/4 "2020-01-11T19:55:42Z")

</div>

> [@philc](#):
>
> df = DataFrame(A = [“a”,“a”,“a”,“b”,“b”,“b”,“c”,“c”],  
> B = [“X”,“Y”,“Z”,“Y”,“Y”,“Z”,“X”,“X”],  
> C = [2,3,5,2,10,7,5,1])

Try something like

```julia
julia> combine(groupby(df, :A), :B=>length∘unique)
3×2 DataFrame
│ Row │ A │ B_function │
│ │ String │ Int64 │
├─────┼────────┼────────────┤
│ 1 │ a │ 3 │
│ 2 │ b │ 2 │
│ 3 │ c │ 1 │

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [January 11, 2020, 9:09pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/5 "2020-01-11T21:09:42Z")

</div>

edit - sorry was thinking of a different API

could also try a countmap([API · OnlineStats Docs](https://joshday.github.io/OnlineStats.jl/stable/api/#OnlineStatsBase.CountMap)), or if the data is small call “unique” on the requisite column and get the length

---

<div class="post-metadata">

**Author:** ![philc](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@philc](https://discourse.julialang.org/u/philc)\
**Post date:** [January 13, 2020, 4:55pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/6 "2020-01-13T16:55:09Z")

</div>

Thanks a lot, simple, elegant, efficient!

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://discourse.julialang.org/u/merlin)\
**Post date:** [July 28, 2021, 10:20pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/7 "2021-07-28T22:20:02Z")

</div>

this is beautiful

---

<div class="post-metadata">

**Author:** ![akshdfyehd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akshdfyehd/32/39097_2.png) [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Post date:** [September 18, 2022, 3:03pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/8 "2022-09-18T15:03:37Z")

</div>

> [@dawbarton](#):
>
> `∘`

Hi, what is the dot between length and unique? how to type it?  
Thanks

---

<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:** [September 18, 2022, 4:01pm UTC](https://discourse.julialang.org/t/how-to-count-the-number-of-categories-present-in-a-column-of-a-dataframe/33244/9 "2022-09-18T16:01:18Z")

</div>

Function composition, type it with `\circ<tab>`. You can always copy paste a unicode symbol into the REPL help mode to find out how to type it.
