当for循环将函数应用于每一行时,如何在python中使用多处理



假设我有一个包含输入数据的panda数据帧,每一行都是一个输入集。目前,我正在对行进行循环,并将结果保存在一个数组中。我想知道这是否可以通过多处理for循环来完成/加快速度。

import pandas as pd
import numpy as np
data = pd.DataFrame([[1, 2],
[3, 4],
[5, 6]])
result = np.zeros((data.shape[0], 2))
for i in range(data.shape[0]):
result[[i], :] = [np.mean(data.iloc[i,:]), np.max(data.iloc[i,:])]

如何使用多处理来制定上述内容?

import concurrent.futures
def do_something():
# your logic code

with concurrent.futures.ProcessPoolExecutor() as executor:
# your_args that you passed to function
your_args = []
results = executor.map(do_something, your_args)

最新更新