Eri*_*sen 8 python csv optimization file pandas
我有一个~50GB的csv文件,我必须这样做
我选择使用Pandas,并且有一种通用的方法来迭代一个方便的块大小(超过五十万行)的块来生成一个DataFrame,并将块附加到每个输出CSV.所以像这样:
_chunk_size = 630100
column_mapping = {
'first_output_specification' : ['Scen', 'MS', 'Time', 'CCF2', 'ESW10'],
# ..... similar mappings for rest of output specifications
}
union_of_used_cols = ['Scen', 'MS', 'Time', 'CCF1', 'CCF2', 'VS', 'ESW 0.00397', 'ESW0.08',
'ESW0.25', 'ESW1', 'ESW 2', 'ESW3', 'ESW 5', 'ESW7', 'ESW 10', 'ESW12',
'ESW 15', 'ESW18', 'ESW 20', 'ESW22', 'ESW 25', 'ESW30', 'ESW 35',
'ESW40']
chnk_iter = pd.read_csv('my_big_csv.csv', header=0, index_col=False,
iterator=True, na_filter=False, usecols=union_of_used_cols)
cnt = 0
while cnt < 100:
chnk = chnk_iter.get_chunk(_chunk_size)
chnk.to_csv('first_output_specification', float_format='%.8f',
columns=column_mapping['first_output_specification'],
mode='a',
header=True,
index=False)
# ..... do the same thing for the rest of the output specifications
cnt += 1
Run Code Online (Sandbox Code Playgroud)
我的问题是,这真的很慢.每个块大约花一分钟生成附加到CSV文件,因此我正在查看将近2个小时完成任务.
我曾尝试通过在读取CSV时使用列子集的并集以及设置来进行一些优化na_filter=False,但它仍然是不可接受的.
我在想,如果有做Python中的CSV文件中,这个光处理更快的方法,无论是通过优化或修正我的做法的手段或者或许仅仅有适合这样的工作,然后大熊猫一个更好的工具..对我来说(一个没有经验的Pandas用户)看起来它和Pandas一样快,但我可能错了.