Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df035_RDFFromPandas.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_dataframe
3## \notebook
4## Read data from Pandas Data Frame into RDataFrame.
5##
6## \macro_code
7## \macro_output
8##
9## \date February 2024
10## \author Pere Mato (CERN)
11
12import ROOT
13import pandas as pd
14
15# Let's create some data in a pandas dataframe
16pdf = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
17
18# Convert the Pandas DataFrame to RDataFrame
19# The column names are directly copied to the RDF
20# Please note that only fundamental types (int, float, ...) are supported and
21# the arrays must have the same length.
22df = ROOT.RDF.FromPandas(pdf)
23
24# You can now use the RDataFrame as usually, e.g. add a column ...
25df = df.Define('z', 'x + y')
26
27# ... or print the content
28df.Display().Print()
29
30# ... or save the data as a ROOT file
31df.Snapshot('tree', 'df035_RDFFromPandas.root')
void Print(GNN_Data &d, std::string txt="")