在下面的代碼中,我正在合并從變量中包含的某個日期開始的所有 csv 文件:file_date。該代碼適用于小型和中等大小的 csv 文件,但在處理非常大的 csv 文件時會崩潰。path = '/Users/Documents/'+file_date+'*'+'-details.csv'+'*' allFiles = glob.glob(path) frame = pd.DataFrame() list_ = [] for file_ in allFiles: frame = pd.read_csv(file_,index_col=None, header=0) print frame.shape list_.append(frame) df = pd.concat(list_) print df.shape df.to_csv('/Users/Documents/'+file_date+'-details.csv',sep=',', index = False)我可以分塊處理每個文件嗎?如果是,我該怎么做?
2 回答

偶然的你
TA貢獻1841條經驗 獲得超3個贊
如果你不處理文件,你甚至不需要熊貓。只需逐行讀取文件并將其寫入新文件:
with open('outfile.csv', 'w') as outfile:
for i, filename in enumerate(all_files):
with open(filename, 'r') as infile:
for rownum, line in enumerate(infile):
if (i != 0) and (rownum == 0): # Only write header once
continue
outfile.write(line + '\n')
添加回答
舉報
0/150
提交
取消