Python 如何将Dask数据帧转换为字典列表?

Python 如何将Dask数据帧转换为字典列表?,python,pandas,dictionary,parallel-processing,dask,Python,Pandas,Dictionary,Parallel Processing,Dask,我需要将dask数据帧转换为字典列表,作为API端点的响应。我知道我可以将dask数据帧转换为pandas,然后从那里转换为dictionary,但最好将每个分区映射到dict,然后连接 我尝试的是: df = dd.read_csv(path, usecols=cols) dd.compute(df.to_dict(orient='records')) 我得到的错误是: AttributeError: 'DataFrame' object has no attribute 'to_dict

我需要将dask数据帧转换为字典列表,作为API端点的响应。我知道我可以将dask数据帧转换为pandas,然后从那里转换为dictionary,但最好将每个分区映射到dict,然后连接

我尝试的是:

df = dd.read_csv(path, usecols=cols)

dd.compute(df.to_dict(orient='records'))
我得到的错误是:

AttributeError: 'DataFrame' object has no attribute 'to_dict'

你可以这样做

import dask.bag as db
db.from_delayed(df.map_partitions(pd.DataFrame.to_dict, orient='records'
    ).to_delayed())
这给了你一个你可以计算(如果它在内存中合适的话)或以其他方式操作的方法

请注意,to_delayed/from_delayed应该不是必需的,还有一个
to_bag
方法,但它似乎做得不对

此外,这里的
dataframe
模型并没有给您带来太多好处,您可能需要从和内置的CSV模块开始