<i id='80Sj2'><tr id='80Sj2'><dt id='80Sj2'><q id='80Sj2'><span id='80Sj2'><b id='80Sj2'><form id='80Sj2'><ins id='80Sj2'></ins><ul id='80Sj2'></ul><sub id='80Sj2'></sub></form><legend id='80Sj2'></legend><bdo id='80Sj2'><pre id='80Sj2'><center id='80Sj2'></center></pre></bdo></b><th id='80Sj2'></th></span></q></dt></tr></i><div id='80Sj2'><tfoot id='80Sj2'></tfoot><dl id='80Sj2'><fieldset id='80Sj2'></fieldset></dl></div>
  • <legend id='80Sj2'><style id='80Sj2'><dir id='80Sj2'><q id='80Sj2'></q></dir></style></legend>

    1. <small id='80Sj2'></small><noframes id='80Sj2'>

          <bdo id='80Sj2'></bdo><ul id='80Sj2'></ul>

        <tfoot id='80Sj2'></tfoot>

        如何在进程之间共享 pandas DataFrame 对象?

        时间:2023-08-06

        <i id='O6Qey'><tr id='O6Qey'><dt id='O6Qey'><q id='O6Qey'><span id='O6Qey'><b id='O6Qey'><form id='O6Qey'><ins id='O6Qey'></ins><ul id='O6Qey'></ul><sub id='O6Qey'></sub></form><legend id='O6Qey'></legend><bdo id='O6Qey'><pre id='O6Qey'><center id='O6Qey'></center></pre></bdo></b><th id='O6Qey'></th></span></q></dt></tr></i><div id='O6Qey'><tfoot id='O6Qey'></tfoot><dl id='O6Qey'><fieldset id='O6Qey'></fieldset></dl></div>
          <bdo id='O6Qey'></bdo><ul id='O6Qey'></ul>
                <tbody id='O6Qey'></tbody>
            1. <legend id='O6Qey'><style id='O6Qey'><dir id='O6Qey'><q id='O6Qey'></q></dir></style></legend>
              <tfoot id='O6Qey'></tfoot>
            2. <small id='O6Qey'></small><noframes id='O6Qey'>

                  本文介绍了如何在进程之间共享 pandas DataFrame 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这个问题与我之前发布的链接有相同的点.

                  This question has the same point of the link that I posted before.

                  ( 有没有避免内存深拷贝或减少多处理时间的好方法?)

                  因为我遇到了DataFrame"对象共享问题,所以我对此一无所知.

                  I'm getting nowhere with that since I faced the 'DataFrame' object sharing problem.

                  我简化了示例代码.

                  如果有任何专业人士修改我的代码以在没有 Manager.list、Manager.dict、numpy sharedmem 的进程之间共享DataFrame"对象,我会非常感谢她或他.

                  If there any professional to revise my code to share 'DataFrame' object between processes without Manager.list, Manager.dict, numpy sharedmem, I will very appreciate to her or him.

                  这是代码.

                  #-*- coding: UTF-8 -*-'
                  import pandas as pd
                  import numpy as np
                  from multiprocessing import *
                  import multiprocessing.sharedctypes as sharedctypes
                  import ctypes
                  
                  def add_new_derived_column(shared_df_obj):
                      shared_df_obj.value['new_column']=shared_df_obj.value['A']+shared_df_obj.value['B'] / 2
                      print shared_df_obj.value.head()
                      '''
                      "new_column" Generated!!!
                  
                            A         B  new_column
                  0 -0.545815 -0.179209   -0.635419
                  1  0.654273 -2.015285   -0.353370
                  2  0.865932 -0.943028    0.394418
                  3 -0.850136  0.464778   -0.617747
                  4 -1.077967 -1.127802   -1.641868
                      '''
                  
                  if __name__ == "__main__":
                  
                      dataframe = pd.DataFrame(np.random.randn(100000, 2), columns=['A', 'B'])
                  
                      # to shared DataFrame object, I use sharedctypes.RawValue
                      shared_df_obj=sharedctypes.RawValue(ctypes.py_object, dataframe )
                  
                      # then I pass the "shared_df_obj" to Mulitiprocessing.Process object
                      process=Process(target=add_new_derived_column, args=(shared_df_obj,))
                      process.start()
                      process.join()
                  
                      print shared_df_obj.value.head()
                      '''
                      "new_column" disappeared.
                      the DataFrame object isn't shared.
                  
                            A         B
                  0 -0.545815 -0.179209
                  1  0.654273 -2.015285
                  2  0.865932 -0.943028
                  3 -0.850136  0.464778
                  4 -1.077967 -1.127802
                      '''
                  

                  推荐答案

                  您可以使用命名空间管理器,以下代码按您的预期工作.

                  You can use a Namespace Manager, the following code works as you expect.

                  #-*- coding: UTF-8 -*-'
                  import pandas as pd
                  import numpy as np
                  from multiprocessing import *
                  import multiprocessing.sharedctypes as sharedctypes
                  import ctypes
                  
                  def add_new_derived_column(ns):
                      dataframe2 = ns.df
                      dataframe2['new_column']=dataframe2['A']+dataframe2['B'] / 2
                      print (dataframe2.head())
                      ns.df = dataframe2
                  
                  if __name__ == "__main__":
                  
                      mgr = Manager()
                      ns = mgr.Namespace()
                  
                      dataframe = pd.DataFrame(np.random.randn(100000, 2), columns=['A', 'B'])
                      ns.df = dataframe
                      print (dataframe.head())
                  
                      # then I pass the "shared_df_obj" to Mulitiprocessing.Process object
                      process=Process(target=add_new_derived_column, args=(ns,))
                      process.start()
                      process.join()
                  
                      print (ns.df.head())
                  

                  这篇关于如何在进程之间共享 pandas DataFrame 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python多处理,传递包含信号量的对象引用 下一篇:Python 多处理 &gt;= 125 列表永远不会完成

                  相关文章

                  最新文章

                    <bdo id='iNuGY'></bdo><ul id='iNuGY'></ul>
                  <i id='iNuGY'><tr id='iNuGY'><dt id='iNuGY'><q id='iNuGY'><span id='iNuGY'><b id='iNuGY'><form id='iNuGY'><ins id='iNuGY'></ins><ul id='iNuGY'></ul><sub id='iNuGY'></sub></form><legend id='iNuGY'></legend><bdo id='iNuGY'><pre id='iNuGY'><center id='iNuGY'></center></pre></bdo></b><th id='iNuGY'></th></span></q></dt></tr></i><div id='iNuGY'><tfoot id='iNuGY'></tfoot><dl id='iNuGY'><fieldset id='iNuGY'></fieldset></dl></div>

                    <tfoot id='iNuGY'></tfoot>
                  1. <legend id='iNuGY'><style id='iNuGY'><dir id='iNuGY'><q id='iNuGY'></q></dir></style></legend>

                      <small id='iNuGY'></small><noframes id='iNuGY'>