# Groupby in RDataFrame

**URL:** <https://root-forum.cern.ch/t/groupby-in-rdataframe/50303>\
**Category:** Evolution Proposals, Apps & Ideas\
**Tags:** rdataframe, feature\
**Created:** [June 7, 2022, 2:29pm UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303 "2022-06-07T14:29:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [June 7, 2022, 2:29pm UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303/1 "2022-06-07T14:29:20Z")

</div>

These days, I do almost all of my analysis using RDataFrame in pyROOT, which is, overall, a very robust and easy-to-use framework. After many many hours with it, my biggest complaint is the lack of [`groupby`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html) capability, which forces me to introduce a pandas dependency and temporarily leave ROOT using the (awkward and slow) `AsNumpy` and `MakeNumpyDataFrame` functions.

@eguiraud’s excellent talk to the LHCb collaboration today on `RDataFrame` makes clear the need for such a feature. Many of the examples he gave in the talk assume that each _event_ is assigned a single row in the datasource, but this is not how standard LHCb software produces TTrees. In LHCb, each _candidate particle_ is assigned a row, so if you want to compare candidates within a given event, a feature like `groupby` (or [`TTreeIndex`](https://root.cern.ch/doc/master/classTTreeIndex.html)) is essential to enable nifty features like index sorting and so forth.

What I would like to see is something like

```python
df = RDataFrame(...)
gdf = df.groupby(["runNumber", "eventNumber"], "ncands")
h_ncands = gdf.Histo1D("ncands") # histogram the number of candidates per event
h_pt = gdf.Histo1D("muon_pt") # identical output to df.Histo1D("muon_pt")
h_pt_first = gdf.Histo1D("muon_pt[0]") # histogram the pt of the first muon in each event

```

where `groupby` creates a new RDataFrame `gdf` with a number of rows equal to the number of unique runNumber and eventNumber combinations. The columns in `gdf` have the same names as the columns in `df` but are now RVecs of length ncands, which is a new column of integers representing the number of rows in `df` with a given combination of runNumber and eventNumber. The runNumber and eventNumber columns alone are not replaced with RVecs and retain their types and values from `df`.

This has come up in a couple other posts ([here](https://root-forum.cern.ch/t/apply-stddev-to-each-bin-with-rdataframe-groupby-feature) and [here](https://root-forum.cern.ch/t/is-groupby-operation-natively-implemented-in-rdataframe)), but since @eguiraud solicited feedback in today’s talk, I thought it would be useful to focus the discussion in a new post.

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [June 7, 2022, 4:24pm UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303/2 "2022-06-07T16:24:42Z")

</div>

I just want to confirm that this would be a very nice feature. I understand it may be complicated to implement.  
I worked in LHCb \>10 years ago, good to see that 1row = 1particle is still used.

---

<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:** [June 8, 2022, 7:54am UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303/3 "2022-06-08T07:54:43Z")

</div>

Hi @mwilkins , @wiso ,

thank you for the feedback, much appreciated. I especially appreciate that you put actual thought in how the feature would look like and work!

I hear you, and I agree it would be nice to have this, the two main challenges are implementing this in a way that does not require that the whole dataset fits in memory and then figuring out how to actually make it work with RDF’s internal implementation, which is not thought for something like this. Point take though!

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![wiso](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wiso/32/11120_2.png) [@wiso](https://root-forum.cern.ch/u/wiso)\
**Post date:** [June 8, 2022, 8:16am UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303/4 "2022-06-08T08:16:41Z")

</div>

Right, I expected this to be complicated, and I think you cannot assume the dataset to be sorted by the grouping key (also because the grouping key could be an expression).

By the way dask can do that, and it should be out of memory and distributed: [dask-examples/02-groupby.ipynb at main · dask/dask-examples · GitHub](https://github.com/dask/dask-examples/blob/main/dataframes/02-groupby.ipynb)

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [June 9, 2022, 7:52am UTC](https://root-forum.cern.ch/t/groupby-in-rdataframe/50303/5 "2022-06-09T07:52:37Z")

</div>

The performance (memory + CPU) of this depends very much on the aggregate. Their doc says

> These are generally fairly efficient, assuming that the number of groups is small (less than a million).

That won’t be enough for many HEP use cases… but we can certainly take some inspiration from what they do!
