Skip to content

Instantly share code, notes, and snippets.

@goddoe
Last active August 23, 2022 17:05
Show Gist options
  • Save goddoe/7795c479be00caa49431443b63dd74e3 to your computer and use it in GitHub Desktop.
Save goddoe/7795c479be00caa49431443b63dd74e3 to your computer and use it in GitHub Desktop.
parallel_apply.py
from functools import partial
import pandas as pd
import numpy as np
def apply(df, func):
return df.apply(func, axis=1)
def parallel_apply(df, func, n_cores):
df_split = np.array_split(df, n_cores)
pool = Pool(n_cores)
series = pd.concat(pool.map(partial(apply, func=func), df_split))
pool.close()
pool.join()
return series
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment