Df 0 3 2_ Make Numpy Data Frame

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, June 15, 2021 at 07:23 AM.

In [ ]:
import ROOT
import numpy as np

Let's create some data in numpy arrays

In [ ]:
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 [ ]:
df = ROOT.RDF.MakeNumpyDataFrame({'x': x, 'y': y})

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

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

... or print the content

In [ ]:

... or save the data as a ROOT file

In [ ]:
df.Snapshot('tree', 'df032_MakeNumpyDataFrame.root')

Draw all canvases

In [ ]:
from ROOT import gROOT