Calculating median with RDataFrame?

Hi all,

Is there an efficient way to get the median of a column using RDataFrame that I am missing?
At the minute the only way I see is to Take the whole column and use numpy/sort it to obtain the median.

import ROOT
import numpy as np

df = ROOT.RDataFrame(10)
df = df.Define("x", "rdfentry_")
x_arr = df.Take["ULong64_t"]("x")
x_arr = x_arr.GetValue()
print(x_arr)
# vector<ULong64_t>{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
np.median(x_arr)

Just wanted to check if there is something else in ROOT’s libraries that might be helpful that I am not using?

Thank you.

Maybe @vpadulan or @jonas can help

Dear @lost_soul_519 ,

Thanks for reaching out to the forum! There is currently no API to directly retrieve the median in RDataFrame, although it could be achieved in a number of ways including what you showed above. I have added a feature request to our tracker to keep track of this Add an API to directly compute median value of a column in RDataFrame · Issue #22794 · root-project/root · GitHub

Cheers,
Vincenzo

There is TMath::Median or TMath::ModeHalfSample