我有超过100K的CSV(总文件大小为150GB)需要加入。虽然列的顺序可能不匹配,并且一些csv缺少一些列,但所有csv都有标准的列名
现在,我刚刚创建了一个数据框架,并在每次迭代中从每个csv中浓缩datframe,以获得一个包含所有列的标准数据框架,我最终打算将其保存为csv
我试着用1000个csv样本制作一个数据框,发现随着数据框大小的增加,迭代次数从每秒10次下降到1.5次,这可能意味着如果我全部使用100k csv,那么它将遵循类似的趋势,因此需要几天甚至几个月的时间来组合它们
有没有更好的方法来组合大量csv文件
这是我的密码
df_t1 = pd.DataFrame()
for i in tqdm(range(len(excelNames))):
thisCSV = str(excelNames[i]).lower().strip()
df = pd.read_csv(pathxl + "\\" + thisCSV, error_bad_lines=False, warn_bad_lines=False,low_memory=False)
df["File Name"] = pd.Series([thisCSV for x in range(len(df.index))])
if thisCSV.endswith('type1.csv'):
df_t1 = pd.concat([df_t1,df], axis=0, ignore_index=True)
df_t1.to_csv(outpath + "df_t1.csv", index = None, header=True, encoding='utf-8')
print("df_t1.csv generated")
可能的改进
方法1:使用熊猫
方法1a
使用熊猫连续附加到CSV输出文件
方法2:二进制文件
读取/写入二进制文件和使用内存映射应该更快
相关问题 更多 >
编程相关推荐