pandas 多处理应用

pandas multiprocessing apply( pandas 多处理应用)
本文介绍了 pandas 多处理应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试对 pandas 数据帧使用多处理,即将数据帧拆分为 8 个部分.使用 apply 对每个部分应用一些功能(每个部分在不同的过程中处理).

I'm trying to use multiprocessing with pandas dataframe, that is split the dataframe to 8 parts. apply some function to each part using apply (with each part processed in different process).

这是我终于找到的解决方案:

Here's the solution I finally found:

import multiprocessing as mp
import pandas.util.testing as pdt

def process_apply(x):
    # do some stuff to data here

def process(df):
    res = df.apply(process_apply, axis=1)
    return res

if __name__ == '__main__':
    p = mp.Pool(processes=8)
    split_dfs = np.array_split(big_df,8)
    pool_results = p.map(aoi_proc, split_dfs)
    p.close()
    p.join()

    # merging parts processed by different processes
    parts = pd.concat(pool_results, axis=0)

    # merging newly calculated parts to big_df
    big_df = pd.concat([big_df, parts], axis=1)

    # checking if the dfs were merged correctly
    pdt.assert_series_equal(parts['id'], big_df['id'])

推荐答案

你可以使用 https://github.com/nalepae/pandarallel,如下例所示:

You can use https://github.com/nalepae/pandarallel, as in the following example:

from pandarallel import pandarallel
from math import sin

pandarallel.initialize()

def func(x):
    return sin(x**2)

df.parallel_apply(func, axis=1)

这篇关于 pandas 多处理应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Python GTK Drag and Drop - Get URL(Python GTK 拖放 - 获取 URL)
Drag n Drop inside QgraphicsView doesn#39;t work (PyQt)(在 QgraphicsView 内拖放不起作用(PyQt))
How to get dropped file names in PyQt/PySide(如何在 PyQt/PySide 中获取删除的文件名)
Drag and drop onto Python script in Windows Explorer(在 Windows 资源管理器中拖放到 Python 脚本)
In Python, how can I include (not import) one file within another file, macro style, without changing namespace?(在 Python 中,如何在不更改命名空间的情况下在另一个文件中包含(不导入)一个文件,宏样式?)
C Preprocessor Macro equivalent for Python(相当于 Python 的 C 预处理器宏)