Logo ROOT  
Reference Guide
df032_MakeNumpyDataFrame.py File Reference

Namespaces

namespace  df032_MakeNumpyDataFrame
 

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.MakeNumpyDataFrame({'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_MakeNumpyDataFrame.root')
void Print(std::ostream &os, const OptionType &opt)
+-----+---+-----------+-----------+
| 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_MakeNumpyDataFrame.py.