Read data from Pandas Data Frame into RDataFrame.
Author: Pere Mato (CERN)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, May 15, 2024 at 09:41 AM.
import ROOT
import pandas as pd
Let's create some data in a pandas dataframe
pdf = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
Convert the Pandas DataFrame to RDataFrame The column names are directly copied to the RDF Please note that only fundamental types (int, float, ...) are supported and the arrays must have the same length.
df = ROOT.RDF.FromPandas(pdf)
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 | 5 | +-----+---+---+---+ | 1 | 2 | 5 | 7 | +-----+---+---+---+ | 2 | 3 | 6 | 9 | +-----+---+---+---+
... or save the data as a ROOT file
df.Snapshot('tree', 'df035_RDFFromPandas.root')
<cppyy.gbl.ROOT.RDF.RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> > object at 0xeafa4d0>
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()