我具有以下功能,将一堆正则表达式应用于数据帧中的每个元素。我将正则表达式应用到的数据帧是一个5MB的块。

def apply_all_regexes(data, regexes):
    # find all regex matches is applied to the pandas' dataframe
    new_df = data.applymap(
        partial(apply_re_to_cell, regexes))
    return regex_applied

def apply_re_to_cell(regexes, cell):
    cell = str(cell)
    regex_matches = []
    for regex in regexes:
        regex_matches.extend(re.findall(regex, cell))
    return regex_matches


由于applymap的串行执行,因此处理所需的时间约为〜elements * (serial execution of the regexes for 1 element)。反正有调用并行性吗?我尝试了ProcessPoolExecutor,但这似乎要比串行执行花费更长的时间。

最佳答案

您是否尝试过将一个大数据帧拆分为多个线程小数据帧,并行应用正则表达式映射并将每个小df重新粘贴在一起?

我能够对基因表达的数据框做类似的事情。
如果您获得预期的输出,我将对其进行小规模控制。

不幸的是我没有足够的声誉来发表评论

def parallelize_dataframe(df, func):
    df_split = np.array_split(df, num_partitions)
    pool = Pool(num_cores)
    for x in df_split:
        print(x.shape)
    df = pd.concat(pool.map(func, df_split))
    pool.close()
    pool.join()


    return df


这是我使用的一般功能

10-06 06:23