Issue
How can I load a csv file which is too big in iPython? It seems that it cannot be loaded at once in memory.
Solution
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
Answered By - Gman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.