是否可以使用生成器加载和写出数据帧

is it possible to use a generator to load and write out a dataframe

我在 pandas 中有一个非常大的数据集,我想将其写入文件。目前我的方法是这样的:

df_dict = df2.to_dict('records')

filename = newfile.json:
for item in df2_dict:
    with open('{0}'.format(filename), 'a+') as outfile:
        json.dump(item,outfile,separators = (',',':'))
        outfile.write('\n')

这非常占用内存。我更愿意做的是如何将 df2 的 1 行转换为字典,然后将其写出到 newfile.json,而不是先将整个 table 转换为字典。但我不知道这是否可能或最好的方法是什么。

您可以使用 Keras ImageDataGenerator Class flow_from_dataframe 方法(https://keras.io/api/preprocessing/image/). I recently used a similar approach for an assignment. This blog could help you get started with it - https://medium.com/@vijayabhaskar96/tutorial-on-keras-flow-from-dataframe-1fd4493d237c