【问题标题】:Pandas Merge vs. Boolean IndexingPandas 合并与布尔索引
【发布时间】:2016-01-26 10:25:23
【问题描述】:

我在 Python 3.4 中使用 pandas 来识别两个数据帧之间的匹配。匹配基于严格相等,但最后一列除外,其中紧密匹配 (+/- 5) 很好。

一个数据框包含许多行,在这种情况下,第二个数据框只是一行。如前所述,所需的结果是包含与行匹配的第一个数据帧的子集的数据帧。

我首先采用了布尔索引的具体解决方案,但这需要一段时间才能遍历所有数据,因此我尝试了 pandas 合并功能。但是,在我的测试数据上,我的合并实现甚至更慢。它的运行速度比布尔索引慢 2 到 4 倍。

这是一个测试运行:

import pandas as pd
import random
import time

def make_lsts(lst, num, num_choices):
    choices = list(range(0,num_choices))
    [lst.append(random.choice(choices)) for i in range(0,num)]
    return lst

def old_way(test, data):
    t1 = time.time()
    tmp = data[(data.col_1 == test.col_1[0]) &
              (data.col_2 == test.col_2[0]) &
              (data.col_3 == test.col_3[0]) &
              (data.col_4 == test.col_4[0]) &
              (data.col_5 == test.col_5[0]) &
              (data.col_6 == test.col_6[0]) &
              (data.col_7 == test.col_7[0]) &
              (data.col_8 >= (test.col_8[0]-5)) &
              (data.col_8 <= (test.col_8[0]+5))]
    t2 = time.time()
    print('old time:', t2-t1)

def new_way(test, data):
    t1 = time.time()
    tmp = pd.merge(test, data, how='inner', sort=False, copy=False,
                   on=['col_1', 'col_2', 'col_3', 'col_4', 'col_5', 'col_6', 'col_7'])
    tmp = tmp[(tmp.col_8_y >= (test.col_8[0] - 5)) & (tmp.col_8_y <= (test.col_8[0] + 5))]
    t2 = time.time()
    print('new time:', t2-t1)

if __name__ == '__main__':
    t1 = time.time()
    data = pd.DataFrame({'col_1':make_lsts([], 4000000, 7),
                         'col_2':make_lsts([], 4000000, 3),
                         'col_3':make_lsts([], 4000000, 3),
                         'col_4':make_lsts([], 4000000, 5),
                         'col_5':make_lsts([], 4000000, 4),
                         'col_6':make_lsts([], 4000000, 4),
                         'col_7':make_lsts([], 4000000, 2),
                         'col_8':make_lsts([], 4000000, 20)})

    test = pd.DataFrame({'col_1':[1], 'col_2':[1], 'col_3':[1], 'col_4':[4], 'col_5':[0], 'col_6':[1], 'col_7':[0], 'col_8':[12]})
    t2 = time.time()
    old_way(test, data)
    new_way(test, data)
    print('time building data:', t2-t1)

在我最近的运行中,我看到以下内容:

 # old time: 0.2209608554840088
 # new time: 0.9070699214935303
 # time building data: 75.05818915367126

请注意,即使具有合并功能的新方法在处理值范围的最后一列上也使用布尔索引,但我认为合并可能能够解决问题。这显然不是这种情况,因为第一列的合并几乎占用了新方法中使用的所有时间。

是否可以优化我的合并功能实现? (来自 R 和 data.table,我花了 30 分钟寻找在 pandas 数据框中设置键的方法,但未成功。)这只是合并不擅长处理的问题吗?为什么在这个例子中布尔索引比合并更快?

我不完全了解这些方法的内存后端,因此感谢任何见解。

【问题讨论】:

    标签: python join pandas merge


    【解决方案1】:

    虽然您可以在任何一组列上进行合并,但在合并索引时,合并的性能将是最佳的。

    如果你替换

    tmp = pd.merge(test, data, how='inner', sort=False, copy=False,
                   on=['col_1', 'col_2', 'col_3', 'col_4', 'col_5', 'col_6', 'col_7'])
    

    cols = ['col_%i' % (i+1) for i in xrange(7)]
    test.set_index(cols, inplace=True)
    data.set_index(cols, inplace=True)
    tmp = pd.merge(test, data, how='inner', left_index=True, right_index=True)
    test.reset_index(inplace=True)
    data.reset_index(inplace=True)
    

    这样跑得更快吗?我还没有测试过,但我认为这应该会有所帮助......

    通过索引您要合并的列,DataFrame 将在后台组织数据,使其知道在哪里找到值比数据仅在普通列中更快。

    【讨论】:

    • 感谢@SPKoder 提供有关索引的提示。这对我来说很有意义,但性能实际上下降了。运行该代码大约需要 7 秒(而不是
    • 嗯。这很令人惊讶。设置 copy=False 会改变什么吗?我对此表示怀疑,但也许值得一试......另外,在这种情况下,结果 DataFrame 是否正确?是否正在创建任何额外的行?
    • 不,使用copy=Falsesort=False对时间没有影响。而且,是的,数据框是正确的。我按照说明 here 更好地索引列,这有助于在一秒钟内将其恢复,但它仍然比布尔索引慢得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多