将文件夹中100w个csv文件,每10w个文件合并转换成10个文件
# -*- coding: utf-8 -*- """ Created on Mon Oct 5 16:01:04 2020 @author: ZXX """ import pandas as pd import numpy as np import os path = files = os.listdir(path) for i, file in enumerate(files): NewName = os.path.join(path, str(i)) OldName = os.path.join(path, file) os.rename(OldName, NewName) for i in range(0,10): for j in range(0,100000): d=[] name=100000*i+j data=pd.read_csv(os.path.join(path, str(name)),header=None) d.append(data) df= pd.DataFrame(d) df.to_csv(os.path.join(path, 'the'+str(i)+'th file'), index=0, header=0) print(i,'is done')
数据背景,文件名为索引值
博主有个处理方法是,先将文件夹中的文件按照按索引重命名(方法参见我之前的文章),因为我本身已经将文件名的有用信息读取过了,这样改,省去文件名复杂的匹配,有大佬有什么好的命名匹配方法也可以分享!~~~
