{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5ccd4ae",
   "metadata": {},
   "source": [
    "# df035_RDFFromPandas\n",
    "Read data from Pandas Data Frame into RDataFrame.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Pere Mato (CERN)  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:10 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6bdc7dbd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:15.716879Z",
     "iopub.status.busy": "2026-05-19T20:10:15.716739Z",
     "iopub.status.idle": "2026-05-19T20:10:16.949164Z",
     "shell.execute_reply": "2026-05-19T20:10:16.948424Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d0f2a5b",
   "metadata": {},
   "source": [
    "Let's create some data in a pandas dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7dfe2d4b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:16.954223Z",
     "iopub.status.busy": "2026-05-19T20:10:16.954028Z",
     "iopub.status.idle": "2026-05-19T20:10:17.070874Z",
     "shell.execute_reply": "2026-05-19T20:10:17.070132Z"
    }
   },
   "outputs": [],
   "source": [
    "pdf = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e446b13",
   "metadata": {},
   "source": [
    "Convert the Pandas DataFrame to RDataFrame\n",
    "The column names are directly copied to the RDF \n",
    "Please note that only fundamental types (int, float, ...) are supported and\n",
    "the arrays must have the same length."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7eae53ca",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:17.072331Z",
     "iopub.status.busy": "2026-05-19T20:10:17.072201Z",
     "iopub.status.idle": "2026-05-19T20:10:18.007988Z",
     "shell.execute_reply": "2026-05-19T20:10:18.007161Z"
    }
   },
   "outputs": [],
   "source": [
    "df = ROOT.RDF.FromPandas(pdf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25c3a539",
   "metadata": {},
   "source": [
    "You can now use the RDataFrame as usually, e.g. add a column ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "771795e0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:18.012230Z",
     "iopub.status.busy": "2026-05-19T20:10:18.012092Z",
     "iopub.status.idle": "2026-05-19T20:10:18.177092Z",
     "shell.execute_reply": "2026-05-19T20:10:18.175754Z"
    }
   },
   "outputs": [],
   "source": [
    "df = df.Define('z', 'x + y')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a99eac7a",
   "metadata": {},
   "source": [
    "... or print the content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4631e38a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:18.179249Z",
     "iopub.status.busy": "2026-05-19T20:10:18.179046Z",
     "iopub.status.idle": "2026-05-19T20:10:19.330686Z",
     "shell.execute_reply": "2026-05-19T20:10:19.329528Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-----+---+---+---+\n",
      "| Row | x | y | z | \n",
      "+-----+---+---+---+\n",
      "| 0   | 1 | 4 | 5 | \n",
      "+-----+---+---+---+\n",
      "| 1   | 2 | 5 | 7 | \n",
      "+-----+---+---+---+\n",
      "| 2   | 3 | 6 | 9 | \n",
      "+-----+---+---+---+\n"
     ]
    }
   ],
   "source": [
    "df.Display().Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dc9fcfc",
   "metadata": {},
   "source": [
    "... or save the data as a ROOT file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4df639bd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:19.340268Z",
     "iopub.status.busy": "2026-05-19T20:10:19.340129Z",
     "iopub.status.idle": "2026-05-19T20:10:20.320152Z",
     "shell.execute_reply": "2026-05-19T20:10:20.319385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cppyy.gbl.ROOT.RDF.RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager> > object at 0x5622094aebf0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.Snapshot('tree', 'df035_RDFFromPandas.root')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "714d65d9",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c5293580",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:20.331309Z",
     "iopub.status.busy": "2026-05-19T20:10:20.331167Z",
     "iopub.status.idle": "2026-05-19T20:10:20.447436Z",
     "shell.execute_reply": "2026-05-19T20:10:20.442895Z"
    }
   },
   "outputs": [],
   "source": [
    "from ROOT import gROOT \n",
    "gROOT.GetListOfCanvases().Draw()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
