# How to efficiently remove zero entries from DataFrame rows

**URL:** <https://root-forum.cern.ch/t/how-to-efficiently-remove-zero-entries-from-dataframe-rows/61750>\
**Category:** Newbie\
**Tags:** tree, python, rdataframe, root\
**Created:** [September 23, 2024, 7:26am UTC](https://root-forum.cern.ch/t/how-to-efficiently-remove-zero-entries-from-dataframe-rows/61750 "2024-09-23T07:26:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terklton](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/terklton/32/37678_2.png) [@Terklton](https://root-forum.cern.ch/u/Terklton)\
**Post date:** [September 23, 2024, 7:26am UTC](https://root-forum.cern.ch/t/how-to-efficiently-remove-zero-entries-from-dataframe-rows/61750/1 "2024-09-23T07:26:58Z")

</div>

Hi all,

I have a dataframe with columns “pp” and “pt”. each row of these columns is a vector and some of the values are 0. I’m trying to find a way to efficiently remove the zeros. I’m using Jupyter Notebooks and Python.  
Example of the structure (where 1 stands for just one row of the dataframe):

1 | 5.40445f |  
| | 0.339785f |  
| | 0.0130158f |  
| | 0.00000f

And I want it to be:  
1 | 5.40445f |  
| | 0.339785f |  
| | 0.0130158f |

Ultimately I just want a histogram of pp vs pt but with all the zeros removed.

Thank you!  
_ROOT Version:_ JupyROOT 6.30/04  
_Platform:_ Jupyter

* * *

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [September 23, 2024, 7:40am UTC](https://root-forum.cern.ch/t/how-to-efficiently-remove-zero-entries-from-dataframe-rows/61750/2 "2024-09-23T07:40:51Z")

</div>

Dear @Terklton ,

Thanks for reaching out to the forum! This is easily achieved in RDataFrame:

```python
import ROOT

# Simply create an example dataset with one row
# using the same values from your post
df = (
    ROOT.RDataFrame(1)
        .Define("vec_withzeros",
                "ROOT::RVecF{5.40445f, 0.339785f, 0.0130158f, 0.00000f}")
)

# Exploit the functionalities of ROOT.RVec, which naturally
# exposes collection data with a familiar numpy-like syntax
df = df.Define("vec_nozeros", "vec_withzeros[vec_withzeros != 0.f]")

df.Display(("vec_withzeros", "vec_nozeros")).Print()

```

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Terklton](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/terklton/32/37678_2.png) [@Terklton](https://root-forum.cern.ch/u/Terklton)\
**Post date:** [September 23, 2024, 7:48am UTC](https://root-forum.cern.ch/t/how-to-efficiently-remove-zero-entries-from-dataframe-rows/61750/3 "2024-09-23T07:48:52Z")

</div>

Thank you, exactly what I was looking for!
