Python Pandas 追加数据框列表

2 投票
1 回答
1461 浏览
提问于 2025-04-18 18:36

这个问题看起来简单,但我觉得之前没有人问过。

如果我有一组数据框(因为要进行多线程处理,所以必须用这种格式),

df_list=[df1,df2,...,dfn]

有没有什么优雅的方法可以把它们全部合并起来?如果能用一行代码实现就更好了。

1 个回答

4

下面这个并行处理的例子可以在 IPython 中运行,使用了 concat 方法:

    from IPython import parallel
    clients = parallel.Client() #a lightweight handle on all the engines of a cluster
    clients.block = True  # use synchronous computations
    print(clients.ids)

    dview = clients[:] #dview = clients.direct_view()
    dview.block = True

    dview.scatter("experiment", myDataFrame) # <myDataFrame> scattered as <experiment> to the engines
    dview["wlist_ptrn"] = wlist_ptrn
    dview.execute("experiment['allFeats'] = experiment.ttext.str.findall(wlist_ptrn)")
    return pd.concat(dview.gather("experiment")) # gather method returns a list of data frames

我希望这个例子对多进程模块的输出有帮助。

撰写回答