Last active
August 23, 2022 17:05
-
-
Save goddoe/7795c479be00caa49431443b63dd74e3 to your computer and use it in GitHub Desktop.
parallel_apply.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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