I am applying a function on a Dataframe column but I want to make it faster as the function takes a lot of processing time when done serially.
df[df['codes']=='None']['q'][:1].apply(lambda x: clf(x,candidate_labels))
Serially a single row just takes 2.52 secs for running but when running the code below using multiprocessing it's taking a lot longer 51.61 secs and I have about 2500 rows for processing so it is going to take a lot of time to run the function. I am looking to speed it up at least 20%.
import multiprocessing
import pandas as pd
import numpy as np
def clf(x):
...
return list
def _apply_df(args):
df, func, kwargs = args
return df.apply(func, **kwargs)
def apply_by_multiprocessing(df, func, **kwargs):
workers = kwargs.pop('workers')
pool = multiprocessing.Pool(processes=workers)
result = pool.map(_apply_df, [(d, func, kwargs)
for d in np.array_split(df, workers)])
pool.close()
return pd.concat(list(result))
if __name__ == '__main__':
tart_time = time.time()
res=apply_by_multiprocessing(df[df['codes']=='None']['q'][:1],clf, workers=4)
print(res)
print("--- %s seconds ---" % (time.time() - start_time))
## run by 4 processors
I have also tried different iterations for multiprocessing but none seem to speed up the flow instaed they slow down my code.
from pandarallel import pandarallel
import time
pandarallel.initialize(progress_bar=True)
start_time = time.time()
categories = df[df['codes']=='None']['q'][:10].parallel_apply(lambda x: clf(x,candidate_labels))
print("--- %s seconds ---" % (time.time() - start_time))
Another experiment:
import multiprocessing as mp
def clf:
...
return list
if __name__ == '__main__':
p = mp.Pool(processes=8)
pool_results = p.map(clf, df[df['codes']=='None']['q'][:1])
p.close()
p.join()