# Shuffle RDataFrame?

**URL:** <https://root-forum.cern.ch/t/shuffle-rdataframe/52062>\
**Category:** ROOT\
**Tags:** rdataframe, feature\
**Created:** [October 23, 2022, 3:27pm UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062 "2022-10-23T15:27:25Z")\
**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:** [October 23, 2022, 3:27pm UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062/1 "2022-10-23T15:27:25Z")

</div>

Continuing the discussion from [Use RDataFrame to produce small training set](https://root-forum.cern.ch/t/use-rdataframe-to-produce-small-training-set/37920/3):

I would like to randomly select a defined number of rows from my `RDataFrame` (a la [`pandas.DataFrame.sample`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html)). Is there yet a way to do this? The above post from 2 years ago said it was in progress.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 25, 2022, 9:23am UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062/2 "2022-10-25T09:23:23Z")

</div>

I think @eguiraud can help you.

---

<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:** [October 26, 2022, 7:35am UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062/3 "2022-10-26T07:35:00Z")

</div>

Hello @mwilkins ,

sorry for the late reply (ACAT…). Random access into a TTree is extremely slow, so we don’t really have something like pandas’ `sample`.

I think the closest thing RDF offers is: `df.Filter("some_rng < some_threshold").Range(n_events)`. The `Range` is to make sure you get _exactly_ the number of events you want, but unfortunately `Range` is only available in single-thread runs.

This summer, in TMVA, we also started the development of a Python API to loop over a dataset and return the data as batches of numpy array, to feed them into ML tools – but that’s not in production yet.

Cheers,  
Enrico

---

<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:** [October 26, 2022, 2:08pm UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062/4 "2022-10-26T14:08:37Z")

</div>

Ah, this is unfortunate. Being able to randomly select rows would be useful for slimming, e.g., calibration datasets where there may be correlations between adjacent rows. I suppose one could create a column of random numbers and use that to randomly select a fraction of the data, as long as you don’t care about getting a precise number of rows at the end.

---

<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:** [November 9, 2022, 2:08pm UTC](https://root-forum.cern.ch/t/shuffle-rdataframe/52062/5 "2022-11-09T14:08:54Z")

</div>

Closed on November 9, 2022, 2:08pm UTC

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