# Is GroupBy operation natively implemented in RDataFrame?

**URL:** <https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [March 10, 2020, 10:20pm UTC](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354 "2020-03-10T22:20:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loris](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@Loris](https://root-forum.cern.ch/u/Loris)\
**Post date:** [March 10, 2020, 10:20pm UTC](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354/1 "2020-03-10T22:20:52Z")

</div>

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ 6.20  
_Platform:_ Mac OS  
_Compiler:_ Clang

Dear ROOT developers,

I am currently analyzing some TTrees employing RDataFrame. Their structure is as follows:

| particle | info\_1 | info\_2 | … | event |
| --- | --- | --- | --- | --- |
| 1 | … | … | … | 1 |
| 2 | … | … | … | 1 |
| 3 | … | … | … | 1 |
| … | | | | |
| 10 | … | … | … | 2 |
| 11 | … | … | … | 2 |

My goal is to perform a GroupBy operation grouping on the **event** column. Please, keep in mind that rows are unordered (e.g. particle 3 could belong to event 2 and particle 4 to event 1).

My current approach is to create a std::map where the key is the event, and the value is a user-defined struct. This map is updated, after applying some filters and definitions, using the `Foreach` function of RDataFrame. Is it possible to perform a GroupBy natively in RDataFrame?  
Moreover, I am considering exploiting multithreading in my analysis, so having such function in RDF API would allow me to take advantage of the implicit MT capability of ROOT.

Best regards,  
Loris

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [March 11, 2020, 8:49am UTC](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354/2 "2020-03-11T08:49:36Z")

</div>

Hi Loris,  
unfortunately `GroupBy` is not implemented in `RDataFrame`, but you can implement it as a [custom action](https://root.cern.ch/doc/master/classROOT_1_1RDF_1_1RInterface.html#a9ed8313806398d106bfc2390301c0408), also for the MT case. Returning a `std::unordered_map` seems like a good way to do it at a first glance.

You could even propose your implementation for inclusion in ROOT inside `ROOT/RDFHelpers.hxx`.

[Here](https://root.cern.ch/doc/master/df018__customActions_8C.html) is a tutorial that shows how to implement a custom action in practice.

I hope this helps!  
Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Loris](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@Loris](https://root-forum.cern.ch/u/Loris)\
**Post date:** [March 12, 2020, 11:01am UTC](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354/3 "2020-03-12T11:01:55Z")

</div>

Hi Enrico,

I see, thank you very much. I will try to polish a bit my code and have a look at it!

Cheers,  
Loris

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 26, 2020, 11:13am UTC](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe/38354/4 "2020-03-26T11:13:54Z")

</div>

Closed on March 26, 2020, 11:13am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
