# How to group by multiple data types?

**URL:** https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872
**Category:** General Usage
**Tags:** dataframes
**Created:** [February 21, 2022, 11:04pm UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872 "2022-02-21T23:04:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)
#### Post date: [February 21, 2022, 11:04pm UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872/1 "2022-02-21T23:04:16Z")

</div>

Hello all,

I’m a bit new to the Julia programming language and haven’t been able to find an answer that solves my problem. I have the following dataframe:

```julia
10×5 DataFrame
 Row │ DATE TOPIC_I TOPIC_J JOINT_PROB DOC_COUNT 
         │ Date String15 String15 Any Any       
──┼─────────────────────────────────────────
   1 │ 2000-09-01 TOPIC_153 TOPIC_87 0.03806723138 979
   2 │ 2000-09-01 TOPIC_81 TOPIC_87 0.01825187194 979
   3 │ 2000-09-01 TOPIC_249 TOPIC_87 0.01616933848 979
   4 │ 2000-09-01 TOPIC_124 TOPIC_87 0.006607188145 979
   5 │ 2000-09-01 TOPIC_140 TOPIC_87 0.0008916937195 979
   6 │ 2000-09-01 TOPIC_101 TOPIC_87 0.001341542903 979
   7 │ 2000-09-01 TOPIC_89 TOPIC_87 0.07842244991 979
   8 │ 2000-09-01 TOPIC_233 TOPIC_87 0.01956784903 979
   9 │ 2000-09-01 TOPIC_144 TOPIC_87 0.01501348474 979
  10 │ 2000-09-01 TOPIC_201 TOPIC_87 0.007407990334 979

```

I am trying to group by the DATE and TOPIC\_I rows, sum the JOINT\_PROB rows and take the average of the DOC\_COUNT rows. I have implemented the code below:

```julia
# Convert the joint probabilities column and document count column to the correct types.
stuff = [typeof(x) for x in probabilities_data[!, :JOINT_PROB]]
println(unique(stuff))

probabilities_data[!, :JOINT_PROB] = [typeof(x) == String ? tryparse(Float64,x) : x for x in probabilities_data[!, :JOINT_PROB]]

stuff = [typeof(x) for x in probabilities_data[!, :JOINT_PROB]]
println(unique(stuff))

p_i_group = groupby(probabilities_data, [:DATE, :TOPIC_I])
pi_df = combine(p_i_group, :TOPIC_J => sum => :PROB_I)

```

I countinue to get the following error related to the last line of code above:

```julia
TaskFailedException:
MethodError: no method matching +(::String15, ::String15)

```

As far as I can tell my syntax is correct. Can someone help me find what I am missing?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 21, 2022, 11:06pm UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872/2 "2022-02-21T23:06:14Z")

</div>

Those are string columns… perhaps you mean to be summing the probabilities?

---

<div class="post-metadata">

### Author: ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)
#### Post date: [February 21, 2022, 11:12pm UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872/3 "2022-02-21T23:12:24Z")

</div>

My apologies. You are correct. I want to sum the “JOINT\_PROB” column. I updated the question to reflect this.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [February 22, 2022, 6:34am UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872/4 "2022-02-22T06:34:44Z")

</div>

Your `JOINT_PROB` and `DOC_COUNT` have both `eltype` `Any` which signals that there is a risk that they do not contain only numbers. If they contain only numbers then the following will work:

```julia
using Statistics
p_i_group = groupby(probabilities_data, [:DATE, :TOPIC_I])
pi_df = combine(p_i_group, :JOINT_PROB => sum => :PROB_I, :DOC_COUNT => mean => :MEAN_COUNT)

```

the easiest way to check if your column contains only numbers is to do e.g. `float.(probabilities_data.JOINT_PROB)`. If this errors this means that you have some bad data in your columns.

---

<div class="post-metadata">

### Author: ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)
#### Post date: [February 22, 2022, 2:45pm UTC](https://discourse.julialang.org/t/how-to-group-by-multiple-data-types/76872/5 "2022-02-22T14:45:16Z")

</div>

It seems there was an issue with the input data. After fixing that and setting the column to “JOINT\_PROB” based on your and @tbeason feedback it looks like the code is working. Thank you both for your help!
