关于巫术的事很抱歉。在
问题是工作线程必须具有唯一的数据帧实例。几乎所有对Pandas数据帧进行切片或分块的尝试都会导致原始数据帧的别名。这些别名仍将导致工作线程之间的资源争用。在
有两件事可以提高性能。首先要确保你和熊猫一起工作。使用iloc或iterrows逐行迭代,这会与数据帧的设计抗衡。使用一个新样式的类对象和applya方法是一种选择。在def get_example_df():
return pd.DataFrame(pd.np.random.randint(10, 100, size=(5,5)))
class Math(object):
def __init__(self):
self.summation = 0
def operation(self, row):
row_result = 0
for elem in row:
if elem % 2:
row_result += elem
else:
row_result += 1
self.summation += row_result
if row_result % 2:
return row_result
else:
return 1
def get_summation(self):
return self.summation
Custom = Math()
df = get_example_df()
df['new_col'] = df.apply(Custom.operation)
print Custom.get_summation()
第二个选项是为每个worker读入或生成每个数据帧。然后根据需要重组。在
^{pr2}$
然而,在大多数情况下不需要多处理。我在合理的时间内(在笔记本电脑上)处理了超过600万行的数据,而没有进行多次处理。在
注意:我没有为上面的代码计时,可能还有改进的余地。在