0

iPythonで大きすぎるcsvファイルをロードするにはどうすればよいですか? 一度にメモリにロードできないようです。

4

1 に答える 1

2

you can use this code to read a file in chunks and it will also distribute the file over multiple processors.

import pandas as pd 
import multiprocessing as mp

LARGE_FILE = "yourfile.csv"
CHUNKSIZE = 100000 # processing 100,000 rows at a time

def process_frame(df):
        # process data frame
        return len(df)

if __name__ == '__main__':
        reader = pd.read_csv(LARGE_FILE, chunksize=CHUNKSIZE)
        pool = mp.Pool(4) # use 4 processes

        funclist = []
        for df in reader:
                # process each data frame
                f = pool.apply_async(process_frame,[df])
                funclist.append(f)

        result = 0
        for f in funclist:
                result += f.get(timeout=10) # timeout in 10 seconds
于 2015-07-21T23:09:06.077 に答える