2017-07-13 175 views
3

因此,我目前有一个目录,我们将其称为/mydir,其中包含36个CSV文件,每个2.1 GB和具有相同的尺寸。他们都是一样的大小,我想将它们读入大熊猫,将它们连接在一起并排侧(行左右的量保持不变),然后输出所产生的数据帧作为一个大的CSV。我为此编写的代码适用于组合其中的一部分,但在某个点后出现内存错误。我想知道是否有比我有更高效的方式来做到这一点。熊猫组合多个CSV和输出为一个大文件

df = pd.DataFrame() 
for file in os.listdir('/mydir'): 
    df.concat([df, pd.read_csv('/mydir' + file, dtype = 'float)], axis = 1) 
df.to_csv('mydir/file.csv') 

有人建议我把它分解成小块,结合文件中的6组,然后结合这些反过来又在一起,但我不知道这是否是一个有效的解决方案,将避免内存错误问题

编辑:查看该目录:

-rw-rw---- 1 m2762 2.1G Jul 11 10:35 2010.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:32 2001.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:28 1983.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:21 2009.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:21 1991.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:07 2000.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:06 1982.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:01 1990.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 10:01 2008.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:55 1999.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:54 1981.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:42 2007.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:42 1998.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:42 1989.csv 
-rw-rw---- 1 m2762 2.1G Jul 11 09:42 1980.csv 
+2

做所有文件具有行的同一#? – MaxU

+0

是的,所有这些尺寸都是相同的,我会更新帖子来指定。 – JSolomonCulp

+1

怎么样使用Linux'paste'工具 - '粘贴-d '' * .CSV> result.csv'? – MaxU

回答

1

大块他们!

from glob import glob 
import os 

# grab files 
files = glob('./[0-9][0-9][0-9][0-9].csv') 

# simplify the file reading 
# notice this will create a generator 
# that goes through chunks of the file 
# at a time 
def read_csv(f, n=100): 
    return pd.read_csv(f, index_col=0, chunksize=n) 

# simplify the concatenation 
def concat(lot): 
    return pd.concat(lot, axis=1) 

# simplify the writing 
# make sure mode is append and header is off 
# if file already exists 
def to_csv(f, df): 
    if os.path.exists(f): 
     mode = 'a' 
     header = False 
    else: 
     mode = 'w' 
     header = True 
    df.to_csv(f, mode=mode, header=header) 

# Fun stuff! zip will take the next element of the generator 
# for each generator created for each file 
# concat one chunk at a time and write 
for lot in zip(*[read_csv(f, n=10) for f in files]): 
    to_csv('out.csv', concat(lot)) 
+0

这似乎已经做到了。以前从未使用过glob,但它像魅力一样工作 – JSolomonCulp

+0

很高兴它成功了! – piRSquared

0

假设答案MaxU是,所有的文件都具有相同的行数,并假设像引用完成了进一步指出,未成年人CSV差异所有文件中的相同方式,你不需要这样做ith熊猫。普通文件readlines会给你可以连接和写出的字符串。进一步假设你可以提供行数。事情是这样的代码:

numrows = 999 # whatever. Probably pass as argument to function or on cmdline 
    out_file = open('myout.csv','w') 
    infile_names = [ 'file01.csv', 
        'file02.csv', 
         .. 
        'file36.csv' ] 

    # open all the input files 
    infiles = [] 
    for fname in infile_names: 
     infiles.append(open(fname)) 

    for i in range(numrows): 
     # read a line from each input file and add it to the output string 
     out_csv='' 
     for infile2read in infiles: 
      out_csv += infile2read.readline().strip() + ',' 
     out_csv[-1] = '\n' # replace final comma with newline 

     # write this rows data out to the output file 
     outfile.write(out_csv) 

    #close the files 
    for f in infiles: 
     f.close() 
    outfile.close()