df032_MakeNumpyDataFrame

Read data from Numpy arrays into RDataFrame.

Author: Stefan Wunsch (KIT, CERN)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, December 08, 2022 at 11:32 AM.

In [1]:
import ROOT
import numpy as np
Welcome to JupyROOT 6.27/01

Let's create some data in numpy arrays

In [2]:
x = np.array([1, 2, 3], dtype=np.int32)
y = np.array([4, 5, 6], dtype=np.float64)

Read the data with RDataFrame The column names in the RDataFrame are defined by the keys of the dictionary. Please note that only fundamental types (int, float, ...) are supported and the arrays must have the same length.

In [3]:
df = ROOT.RDF.MakeNumpyDataFrame({'x': x, 'y': y})

You can now use the RDataFrame as usually, e.g. add a column ...

In [4]:
df = df.Define('z', 'x + y')

... or print the content

In [5]:
df.Display().Print()
+-----+---+-----------+-----------+
| Row | x | y         | z         | 
+-----+---+-----------+-----------+
| 0   | 1 | 4.0000000 | 5.0000000 | 
+-----+---+-----------+-----------+
| 1   | 2 | 5.0000000 | 7.0000000 | 
+-----+---+-----------+-----------+
| 2   | 3 | 6.0000000 | 9.0000000 | 
+-----+---+-----------+-----------+

... or save the data as a ROOT file

In [6]:
df.Snapshot('tree', 'df032_MakeNumpyDataFrame.root')
Out[6]:
<cppyy.gbl.ROOT.RDF.RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0xb524530>

Draw all canvases

In [7]:
from ROOT import gROOT 
gROOT.GetListOfCanvases().Draw()