本文介紹了如何在進程之間共享 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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!