如何将 pandas 数据框转换为箭头数据集?

Zen*_*ven 7 pandas pyarrow

在huggingface库中,有一种特殊格式的数据集,称为箭头数据集

https://arrow.apache.org/docs/python/dataset.html

https://huggingface.co/datasets/wiki_lingua

我必须将普通的 pandas 数据框转换为数据集或读取表格 csv 文件作为数据集。

那可能吗?

TDr*_*bas 10

您可以创建一个pyarrow.Table,然后将其转换为Dataset. 这是一个例子。

\n
import pyarrow as pa\nimport pyarrow.dataset as ds\nimport pandas as pd\nfrom datasets import Dataset\n\ndf = pd.DataFrame({\'a\': [0,1,2], \'b\': [3,4,5]})\ndataset = ds.dataset(pa.Table.from_pandas(df).to_batches())\n\n### convert to Huggingface dataset\nhg_dataset = Dataset(pa.Table.from_pandas(df))\n
Run Code Online (Sandbox Code Playgroud)\n

要转换为Tableonly,您可以使用from_pandas(\xe2\x80\xa6)文档和上面示例中所示的方法。https://arrow.apache.org/docs/python/pandas.html

\n

对 Huggingface 文档的引用:https://huggingface.co/docs/datasets/package_reference/main_classes.html#dataset

\n