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 Tuesday, March 19, 2024 at 07:07 PM.
import ROOT
import numpy as np
Let's create some data in numpy arrays
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.
df = ROOT.RDF.FromNumpy({'x': x, 'y': y})
You can now use the RDataFrame as usually, e.g. add a column ...
df = df.Define('z', 'x + y')
... or print the content
df.Display().Print()
+-----+---+----------+----------+ | Row | x | y | z | +-----+---+----------+----------+ | 0 | 1 | 4.000000 | 5.000000 | +-----+---+----------+----------+ | 1 | 2 | 5.000000 | 7.000000 | +-----+---+----------+----------+ | 2 | 3 | 6.000000 | 9.000000 | +-----+---+----------+----------+
... or save the data as a ROOT file
df.Snapshot('tree', 'df032_RDFFromNumpy.root')
<cppyy.gbl.ROOT.RDF.RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0xcc2a170>
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()