# Adding a column to dataframe without changing its name

**URL:** <https://root-forum.cern.ch/t/adding-a-column-to-dataframe-without-changing-its-name/62174>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [November 8, 2024, 10:33am UTC](https://root-forum.cern.ch/t/adding-a-column-to-dataframe-without-changing-its-name/62174 "2024-11-08T10:33:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amansas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@amansas](https://root-forum.cern.ch/u/amansas)\
**Post date:** [November 8, 2024, 10:33am UTC](https://root-forum.cern.ch/t/adding-a-column-to-dataframe-without-changing-its-name/62174/1 "2024-11-08T10:33:05Z")

</div>

Hello. So I was playing around with data frames and wanted to know how I can add columns to my existing dataframe without having to create/define a new one. For eg. let’s say I have a dataframe `df0` with the columns `x,y,z` and wanted to add a new column `t=y-x`. The code `df0.Define("t", "y-x")` does nothing. I am running this in interactive mode/directly on the terminal after running `root -l` so any help will be appreciated.

---

<div class="post-metadata">

**Author:** ![leadreyfus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/leadreyfus/32/37916_2.png) [@leadreyfus](https://root-forum.cern.ch/u/leadreyfus)\
**Post date:** [November 8, 2024, 10:59am UTC](https://root-forum.cern.ch/t/adding-a-column-to-dataframe-without-changing-its-name/62174/2 "2024-11-08T10:59:09Z")

</div>

Hello,  
You just need to assign `df0` to the result of `Define()`, ie:

```auto
df0 = df0.Define("t", "y-x")

```

Here’s a minimal example using the terminal:

```auto
root [0] ROOT::RDataFrame rdf(100);
root [1] auto rdf_x = rdf.Define("x", [](){ return gRandom->Rndm(); });
root [2] rdf_x.Display()->Print()
+-----+----------+
| Row | x | 
+-----+----------+
| 0 | 0.999742 | 
+-----+----------+
| 1 | 0.162910 | 
+-----+----------+
| 2 | 0.282618 | 
+-----+----------+
| 3 | 0.947201 | 
+-----+----------+
| 4 | 0.231657 | 
+-----+----------+
root [3] rdf_x = rdf_x.Define("y", [](){ return gRandom->Rndm(); });
root [4] rdf_x.Display()->Print()
+-----+----------+----------+
| Row | x | y | 
+-----+----------+----------+
| 0 | 0.484974 | 0.957477 | 
+-----+----------+----------+
| 1 | 0.744305 | 0.540044 | 
+-----+----------+----------+
| 2 | 0.739953 | 0.759944 | 
+-----+----------+----------+
| 3 | 0.658637 | 0.315638 | 
+-----+----------+----------+
| 4 | 0.804403 | 0.519672 | 
+-----+----------+----------+
root [5] rdf_x = rdf_x.Define("t", "y-x")
(ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager, void> &) @0x7fef6de0a000
root [6] rdf_x.Display()->Print()
+-----+-----------+----------+----------+
| Row | t | x | y | 
+-----+-----------+----------+----------+
| 0 | 0.306957 | 0.168572 | 0.475530 | 
+-----+-----------+----------+----------+
| 1 | -0.170646 | 0.392314 | 0.221668 | 
+-----+-----------+----------+----------+
| 2 | -0.182855 | 0.213190 | 0.030335 | 
+-----+-----------+----------+----------+
| 3 | -0.139390 | 0.333539 | 0.194149 | 
+-----+-----------+----------+----------+
| 4 | -0.363785 | 0.943717 | 0.579932 | 
+-----+-----------+----------+----------+

```
