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

Detailed Description

View in nbviewer Open in SWAN
Read data from Pandas Data Frame into RDataFrame.

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()
# ... or save the data as a ROOT file
df.Snapshot('tree', 'df035_RDFFromPandas.root')
void Print(GNN_Data &d, std::string txt="")
+-----+---+---+---+
| Row | x | y | z |
+-----+---+---+---+
| 0 | 1 | 4 | 5 |
+-----+---+---+---+
| 1 | 2 | 5 | 7 |
+-----+---+---+---+
| 2 | 3 | 6 | 9 |
+-----+---+---+---+
Date
February 2024
Author
Pere Mato (CERN)

Definition in file df035_RDFFromPandas.py.