Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert Pandas DataFrame to a Huggingface Dataset object?

I have the following df:

import pandas as pd
df = pd.DataFrame({"foo": ["bar", "baz"]})

How do I convert to a Huggingface Dataset?

like image 460
Vincent Claes Avatar asked Dec 04 '25 13:12

Vincent Claes


1 Answers

datasets have an easy way to convert pandas dataframes to hugginface datasets:

from datasets import Dataset
dataset = Dataset.from_pandas(df)

# if you want to go back to a pandas dataframe
df = dataset.to_pandas()

more info here: https://huggingface.co/docs/datasets/main/en/loading#inmemory-data

like image 54
Vincent Claes Avatar answered Dec 06 '25 14:12

Vincent Claes