Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df032_RDFFromNumpy.py File Reference

Namespaces

namespace  df032_RDFFromNumpy
 

Detailed Description

View in nbviewer Open in SWAN
Read data from Numpy arrays into RDataFrame.

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()
# ... or save the data as a ROOT file
df.Snapshot('tree', 'df032_RDFFromNumpy.root')
+-----+---+-----------+-----------+
| Row | x | y | z |
+-----+---+-----------+-----------+
| 0 | 1 | 4.0000000 | 5.0000000 |
+-----+---+-----------+-----------+
| 1 | 2 | 5.0000000 | 7.0000000 |
+-----+---+-----------+-----------+
| 2 | 3 | 6.0000000 | 9.0000000 |
+-----+---+-----------+-----------+
Date
March 2021
Author
Stefan Wunsch (KIT, CERN)

Definition in file df032_RDFFromNumpy.py.