pandas 在python中使用Parallel和delayed函数时,无法获得2个dataframe输出

kgsdhlau  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(84)

我想在Python中进行多处理或并行处理。我写了下面的代码。

import pandas as pd
import multiprocessing
from joblib import Parallel, delayed
from tqdm import tqdm

num_cores = multiprocessing.cpu_count()

df = pd.DataFrame([["A",5],["B",4],["C",7]],columns=["item","val"])
inputs = ["A","B"]

def my_function(inputs):
    for unique_id in inputs:
        df3 code
        df4 code
    return (df3,df4)

if __name__ == "__main__":
    df3,df4 = Parallel(n_jobs=num_cores)(delayed(my_function)(i) for i in inputs)```

I am able to get df3 and df4 output if save to csv file but while returning 2 variables I am getting following error:

***ValueError: not enough values to unpack (expected 2, got 1)***

What can be the possible reason? How to resolve it?
tpgth1q7

tpgth1q71#

你的代码不清楚。但是,您可以尝试使用zippd.concat

df3, df4 = zip(*Parallel(n_jobs=num_cores)(delayed(my_function)(i) for i in inputs))
df3 = pd.concat(df3)
df4 = pd.concat(df4)

相关问题