【问题标题】:Python Pandas removing substring using another columnPython Pandas 使用另一列删除子字符串
【发布时间】:2016-04-18 19:38:45
【问题描述】:

我已经尝试过四处搜索,但找不到一个简单的方法来做到这一点,所以我希望你的专业知识能提供帮助。

我有一个包含两列的 pandas 数据框

import numpy as np
import pandas as pd

pd.options.display.width = 1000
testing = pd.DataFrame({'NAME':[
    'FIRST', np.nan, 'NAME2', 'NAME3', 
    'NAME4', 'NAME5', 'NAME6'], 'FULL_NAME':['FIRST LAST', np.nan, 'FIRST LAST', 'FIRST NAME3', 'FIRST NAME4 LAST', 'ANOTHER NAME', 'LAST NAME']})

这给了我

          FULL_NAME   NAME
0        FIRST LAST  FIRST
1               NaN    NaN
2        FIRST LAST  NAME2
3       FIRST NAME3  NAME3
4  FIRST NAME4 LAST  NAME4
5      ANOTHER NAME  NAME5
6         LAST NAME  NAME6

我想做的是从“NAME”列中获取值,然后从“FULL NAME”列中删除(如果存在)。所以函数会返回

          FULL_NAME   NAME           NEW
0        FIRST LAST  FIRST          LAST
1               NaN    NaN           NaN
2        FIRST LAST  NAME2    FIRST LAST
3       FIRST NAME3  NAME3         FIRST
4  FIRST NAME4 LAST  NAME4    FIRST LAST
5      ANOTHER NAME  NAME5  ANOTHER NAME
6         LAST NAME  NAME6     LAST NAME

到目前为止,我已经在下面定义了一个函数,并且正在使用 apply 方法。不过,这在我的大型数据集上运行相当慢,我希望有一种更有效的方法来做到这一点。谢谢!

def address_remove(x):
    try:
        newADDR1 = re.sub(x['NAME'], '', x[-1])
        newADDR1 = newADDR1.rstrip()
        newADDR1 = newADDR1.lstrip()
        return newADDR1
    except:
        return x[-1]

【问题讨论】:

    标签: python string pandas replace series


    【解决方案1】:

    这是一种比您当前的解决方案快得多的解决方案,但我不相信不会有更快的解决方案

    In [13]: import numpy as np
             import pandas as pd
             n = 1000
             testing  = pd.DataFrame({'NAME':[
             'FIRST', np.nan, 'NAME2', 'NAME3', 
             'NAME4', 'NAME5', 'NAME6']*n, 'FULL_NAME':['FIRST LAST', np.nan, 'FIRST  LAST', 'FIRST NAME3', 'FIRST NAME4 LAST', 'ANOTHER NAME', 'LAST NAME']*n})
    

    这是一种很长的单线,但它应该可以满足您的需求

    我能想出的快速解决方案是使用另一个答案中提到的replace

    In [37]: %timeit testing ['NEW2'] = [e.replace(k, '') for e, k in zip(testing.FULL_NAME.astype('str'), testing.NAME.astype('str'))]
    100 loops, best of 3: 4.67 ms per loop
    

    原答案:

    In [14]: %timeit testing ['NEW'] = [''.join(str(e).split(k)) for e, k in zip(testing.FULL_NAME.astype('str'), testing.NAME.astype('str'))]
    100 loops, best of 3: 7.24 ms per loop
    

    与您当前的解决方案相比:

    In [16]: %timeit testing['NEW1'] = testing.apply(address_remove, axis=1)
    10 loops, best of 3: 166 ms per loop
    

    这些为您提供与当前解决方案相同的答案

    【讨论】:

    • 太棒了!我试图提出第二个解决方案,但第三个更好!你介意告诉我“zip”命令在做什么吗?
    • 很高兴成功了! zip 接受多个迭代并从原始迭代返回聚合的迭代器。在更通俗的术语中,它允许您同时循环遍历两个或多个可迭代对象。 docs.python.org/3/library/functions.html#zip
    【解决方案2】:

    您可以使用replace 方法和regex 参数,然后使用str.strip

    In [605]: testing.FULL_NAME.replace(testing.NAME[testing.NAME.notnull()], '', regex = True).str.strip()
    Out[605]: 
    0            LAST
    1             NaN
    2      FIRST LAST
    3           FIRST
    4     FIRST  LAST
    5    ANOTHER NAME
    6       LAST NAME
    Name: FULL_NAME, dtype: object
    

    注意您需要将notnull 传递给testing.NAME,因为没有它NaN 值也将被替换为空字符串

    Benchmarking 比最快的@johnchase 解决方案要慢,但我认为它更具可读性并使用 DataFrames 和 Series 的所有 pandas 方法:

    In [607]: %timeit testing['NEW'] = testing.FULL_NAME.replace(testing.NAME[testing.NAME.notnull()], '', regex = True).str.strip()
    100 loops, best of 3: 4.56 ms per loop
    
    In [661]: %timeit testing ['NEW'] = [e.replace(k, '') for e, k in zip(testing.FULL_NAME.astype('str'), testing.NAME.astype('str'))]
    1000 loops, best of 3: 450 µs per loop
    

    【讨论】:

    • 纯熊猫解决方案。干得好。绝对更容易阅读,即使它不是更快。
    • @johnchase 是的,对不起。这是为了减少在控制台中输入的次数
    • 是的,我一开始也做了同样的事情。另外,您的测试的数据框大小是多少?我在运行您的代码时得到了完全不同的计时结果,但我想知道这是否是我正在做的事情......
    • @johnchase 是的,您的解决方案几乎快 10 倍。我有一台更强大的 PC :)
    【解决方案3】:

    我认为您想使用字符串具有的 replace() 方法,它比使用正则表达式快几个数量级(我刚刚在 IPython 中快速检查过):

    %timeit mystr.replace("ello", "")
    The slowest run took 7.64 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000000 loops, best of 3: 250 ns per loop
    
    %timeit re.sub("ello","", "e")
    The slowest run took 21.03 times longer than the fastest. This could mean that an intermediate result is being cached 
    1000000 loops, best of 3: 4.7 µs per loop
    

    如果在那之后您需要进一步提高速度,您应该研究 numpy 的 vectorize 函数(但我认为使用替换而不是正则表达式的速度应该相当可观)。

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 2020-10-12
      • 2014-12-23
      • 2012-01-31
      • 2020-06-14
      • 2019-07-20
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多