大数据框上的熊猫合并速度很慢

时间:2019-12-27 02:50:06

标签: python-3.x pandas

我有4个pandas数据帧(每个3200万行)。我想以合并数据框只从所有数据框添加新变量的方式来连接这些数据框。

例如:

df1 = pd.DataFrame(data = {'col1' : [1, 2, 3],
                           'col2' : [10, 11, 12], 'col3' : [1, 1, 2], 'col4' : [100, 200, 300]})

df1

   col1  col2  col3  col4
0     1    10     1   100
1     2    11     1   200
2     3    12     2   300

df2 = pd.DataFrame(data = {'col1' : [1, 2, 3],
                           'col2' : [10, 11, 12], 'col3' : [1, 1, 2], 'col5' : [20, 40, 60]})

   col1  col2  col3  col5
0     1    10     1    20
1     2    11     1    40
2     3    12     2    60

df3 = pd.DataFrame(data = {'col1' : [1, 2, 3],
                           'col2' : [10, 11, 12], 'col3' : [1, 1, 2], 'col6' : [90, 100, 110]})

   col1  col2  col3  col6
0     1    10     1    90
1     2    11     1    100
2     3    12     2    100

我的预期输出数据框:

   col1  col2  col3   col4  col5 col6
0     1    10     1  100.0   20   90
1     2    11     1  200.0   40   100
2     3    12     2  300.0   60   110

我尝试了下面的代码,它工作得很好,但是非常慢(因为我的数据帧很大)

df_merge=df1.merge(df2.merge(df3))
print(df_merge)

   col1  col2  col3  col4  col5  col6
0     1    10     1   100    20   90
1     2    11     1   200    40   100
2     3    12     2   300    60   110

有什么有效的方法可以做到吗?

1 个答案:

答案 0 :(得分:0)

我不知道它是否值得,但是我听说过modin,这是大数据集的熊猫: https://github.com/modin-project/modin