gpt4 book ai didi

python - Pandas 蟒 : Writing output to specific cells in the CSV

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:42 24 4
gpt4 key购买 nike

我正在尝试将 pandas df 写入 csv。但是,我需要循环很多次才能分块输出。我想在 csv 中垂直堆叠输出。因此,在每次迭代之后,我想写入一组特定的行(1:10,然后 11:20,等等)并清除内存,以免形成一个巨大的对象。是否可以使用 df.to_csv 来执行此操作?

最佳答案

to_csv接受要追加的模式参数:

import numpy as np
# df.shape returns the dimensions in a tuple, the first dimension is the number of rows
df_list = np.array_split(df, df.shape[0]/10)
for d in df_list:
d.to_csv(csv_path, mode='a')

您可以使用 numpy array split 生成一个 df 列表,每行分成 10 行,然后将它们写出或您需要做的任何事情以节省内存

关于python - Pandas 蟒 : Writing output to specific cells in the CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com