# Saving a large RDF distributed with Dask to CSV

**URL:** <https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610>\
**Category:** ROOT\
**Tags:** python, rdataframe\
**Created:** [May 26, 2025, 9:28am UTC](https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610 "2025-05-26T09:28:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asradhak](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ba8739/32.png) [@asradhak](https://root-forum.cern.ch/u/asradhak)\
**Post date:** [May 26, 2025, 9:28am UTC](https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610/1 "2025-05-26T09:28:56Z")

</div>

I’m working with ROOT’s RDataFrame distributed with Dask to process very large datasets (terabyte scale). I want to save selected branches of a distriburted\_rdf to a CSV file, but memory usage becomes a bottleneck when converting the RDF to a pandas DataFrame. Here’s a minimal snippet:

```auto
df = ROOT.RDF.Experimental.Distributed.Dask.RDataFrame(
    tree_name, root_file_list, daskclient=client, npartitions=800
)

chunk_size = 400000
for start in range(0, num_entries, chunk_size):
    end = min(start + chunk_size, num_entries)
    df_pd = pd.DataFrame({col: list(df.AsNumpy([col])[col]) for col in column_order})
    df_pd.to_csv("output.csv", index=False, mode='a', header=(start == 0))

```

I have also tried other approaches like

- **Dask Delayed to DataFrame** , then `.compute()` to pandas — still memory-heavy.
- **Manual partition writing** after `client.gather()` — doesn’t scale well.
- However, chunking significantly slows down the process and is now the main bottleneck.

🔗 **Full notebook:** /github.com/asrithhep/Anomaly-detection-with-transformer-model-in-HEP/blob/main/RDataframe\_main.ipynb (see **cell 8** for the problem area)

Is there a way to directly save a ROOT `RDataFrame` (distributed with Dask) to CSV efficiently _without_ converting the full dataset to pandas or hitting memory constraints? Any suggestions for optimizing this IO operation while maintaining performance for TB-scale data?  
_ROOT Version:_ 6.34  
Platform:\_ swan.cern.ch

_ **Other methods** _

1. **Dask Delayed + Dask DataFrame conversion:**

```auto
# Convert dict to delayed DataFrame
delayed_df = dask.delayed(pd.DataFrame)(data_dict)

# Wrap into Dask dataframe
dask_df = dd.from_delayed([delayed_df])
dask_df = dask_df.repartition(npartitions=1000).persist()

# Compute and write to CSV
df_pd = dask_df.compute()
df_pd.to_csv("output.csv", index=False, mode=mode, header=header)

```

1. **Manual partition writer:**

```auto
def write_partition(partition_id, data):
    import os
    output_dir = "csv_chunks"
    os.makedirs(output_dir, exist_ok=True)
    filename = os.path.join(output_dir, f"part_{partition_id:04d}.csv")
    
    with open(filename, "w") as f:
        f.write(",".join(columns) + "\n")
        for row in zip(*[data[col] for col in columns]):
            f.write(",".join(map(str, row)) + "\n")
    return filename

partition_data = client.gather(df.AsNumpy(columns))
written_files = [write_partition(0, partition_data)]

```

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [May 26, 2025, 11:56am UTC](https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610/2 "2025-05-26T11:56:22Z")

</div>

First, welcome to the ROOT Forum!  
Then I think maybe @vpadulan can help

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [June 5, 2025, 9:58am UTC](https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610/3 "2025-06-05T09:58:27Z")

</div>

Just posting here for others who might find this topic.

> [@asradhak](#):
>
> Is there a way to directly save a ROOT `RDataFrame` (distributed with Dask) to CSV efficiently _without_ converting the full dataset to pandas or hitting memory constraints?

As discussed in person, TB-scale CSV is absolutely not advisable. Due to ROOT’s very efficient compression, the dataset would blow up to several its current size.  
For looking at a few events, the way to go via numpy is viable, but when feeding ML, something like a batch generator from ROOT directly to ML is a better way forward: [ROOT: tutorials/machine\_learning/RBatchGenerator\_PyTorch.py File Reference](https://root.cern/doc/master/RBatchGenerator__PyTorch_8py.html)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 19, 2025, 9:59am UTC](https://root-forum.cern.ch/t/saving-a-large-rdf-distributed-with-dask-to-csv/63610/4 "2025-06-19T09:59:18Z")

</div>

Closed on June 19, 2025, 9:59am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
