Pandas:改善循环字符串包含子字符串的运行时间

时间:2018-02-17 13:30:11

标签: python pandas dataframe substring

我有一个Pandas数据框,其中包含一个包含很长字符串的列(比如说URL_paths)和一个唯一子字符串列表(引用列表)。对于我的数据框中的每一行,我想确定列表中的相应引用元素。因此,如果给定行中的网址为abcd1234,其中一个参考值为cd123,那么我想添加cd123作为对我的数据框的引用,以对此进行分类行/ URL。

我的代码工作正常(参见下面的示例),但是由于for循环(我猜)它很慢,我无法摆脱它。我觉得我的代码可以更快,但却想不到改进它的方法。

如何改善运行时间?

见下面的工作示例:

import string
import secrets
import pandas as pd
import time
from random import randint

n_ref = 100
n_target = 1000000

## Build reference Series, and target dataframe
reference = pd.Series(''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(randint(10, 19))) 
                      for _ in range(n_ref))

target = pd.Series(reference.sample(n = n_target, replace = True)).reset_index().iloc[:,1]

dfTarget = pd.DataFrame({
        'target' : target,
        'pre-string' : pd.Series(''.join(secrets.choice(string.ascii_uppercase + string.digits) 
                                    for _ in range(randint(1, 10))) 
                                    for _ in range(n_target)),
        'post-string' : pd.Series(''.join(secrets.choice(string.ascii_uppercase + string.digits) 
                                    for _ in range(randint(1, 10))) 
                                    for _ in range(n_target)),
        'reference' : pd.Series()})

dfTarget['target_combined'] = dfTarget[['pre-string', 'target', 'post-string']].apply(lambda x: ''.join(x), axis=1)

## Fill in reference column
## Loop over references and return reference in reference column

start_time = time.time()
for x in reference:
    dfTarget.loc[dfTarget['target_combined'].str.contains(x) == True, 'reference'] = x
print("--- %s seconds ---" % (time.time() - start_time))

出:42.60... seconds

2 个答案:

答案 0 :(得分:4)

在我的机器上,我看到使用pd.Series.apply提高了17倍:

reference_set = set(reference)

def calculator(x):
    return next((i for i in reference_set if i in x), None)

dfTarget['reference'] = dfTarget['target_combined'].apply(calculator)

但为了获得最佳效果,请参阅@unutbu's solution

答案 1 :(得分:2)

以下是一种略微(4.3倍)的快速方法:

RegEx模式:

In [23]: pat = '.*({}).*'.format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8DO|WMLKKETELHC|WDPHDS7A7XN7|6X4O4AE2IB3OS|V5J5HWO9RO19ZW2LGT|MK9
P8D9N8V4AJZB|0VT48C38I4T1V6S|R987QUQBTPRHCT7QWA4|D4XXBMCYWQ1172OY|ZUY1O565D2W5GSAL8|V8AR792X1K5UL9DLCKV|CXYK6IQWK3MUC3CO|6X7B6240VC9YL|4QV2D
13ZY15A9D5M1H|WJ7HOMK2FNBZZ6N2Z|QCOWSA3RLR|81I6Z0I5GM|KRD9Y1H3E2WEY9710Q|0161MNQHKEC30E8UI|HGB4XB0QDVHM4H92|RWD6L6EZJUSRK|6U9WOE3YVYKY31K8Q0
K|KCXWHL43B16MRQ1|EO330WAPN7XMX4|VYUX5W2NN277W09NMDB|J8EXE4YIMN0FB|SHE8D14C5A3X|PMPYKSY2FVXFR4Y8X3W|G3YU894U5QGOOM3Z|58J37WJPJBOC7QNKV|NE9WE
JSRXTYFXYZ0TBI|7UPR5XSVOJ244HHZ|N0QZCN6NADW|W2CTEUISOHUY).*'

的更换:

dfTarget['reference'] = dfTarget['target_combined'].str.replace(pat, r'\1')

时间对抗10.000行DF:

In [25]: %%timeit
    ...: dfTarget['reference'] = dfTarget['target_combined'].str.replace(pat, r'\1')
    ...:
617 ms ± 2.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [26]: %%timeit
    ...: [dfTarget.loc[dfTarget['target_combined'].str.contains(x) == True, 'reference'] for x in reference]
    ...:
1.96 s ± 2.08 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [27]: %%timeit
    ...: for x in reference:
    ...:     dfTarget.loc[dfTarget['target_combined'].str.contains(x) == True, 'reference'] = x
    ...:
2.64 s ± 14.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [28]: 2.64/0.617
Out[28]: 4.278768233387359

In [29]: 2.64/1.96
Out[29]: 1.3469387755102042