I have an application which would read say 50 large size csvs file around 400MB each. Now i am reading these to create a dataframe and ultimately concatenate all these into 1 single dataframe. I want to do that in parallel to speed up the overall process. So my code below looks something like this:
import numpy as np
import pandas as pd
from multiprocessing.pool import ThreadPool
from time import time
Class dataProvider:
def __init__(self):
self.df=pd.DataFrame()
self.pool = ThreadPool(processes=40)
self.df_abc=pd.DataFrame()
self.df_xyz=pd.DataFrame()
self.start=time()
def get_csv_data(self,filename):
return pd.read_csv(filename)
def get_all_csv_data(self,filename):
self.start=time()
df_1 = self.pool.apply_sync(self.get_csv_data,('1.csv',), callback=concatDf)
df_2 = self.pool.apply_sync(self.get_csv_data,('2.csv',), callback=concatDf)
total_time=time()-self.start
def concatDf(self):
self.df_abc=pd.concat([df_1,df_2])
self.df_xyz=self.df_abc.iloc[:,1:]
return self.df_xyz
I see below problem with the code:
Thanks
Edit: Use this solution only if you have enough RAM available.
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import pandas as pd
from glob import glob
files = glob("*.csv")
def read_file(file):
return pd.read_csv(file)
# I would recommend to try out whether ThreadPoolExecutor or
# ProcessPoolExecutor is faster on your system:
with ThreadPoolExecutor(4) as pool:
df = pd.concat(pool.map(read_file, files))
print(df)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With