Python Pandas使用另一列删除子字符串

2024-10-03 06:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着四处寻找,但找不到一个简单的方法,所以我希望你的专业知识能有所帮助。在

我有一个有两列的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']})

这给了我

^{pr2}$

我想做的是从“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]

Tags: 数据方法nameimportpandasnpanothernan
3条回答

但我不相信有比现在更快的解决方案

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,如另一个答案中所述:

^{pr2}$

原始答案:

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

这些方法可以得到与当前解决方案相同的答案

我想您应该使用string所具有的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的向量化函数(但是我认为使用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值也将被替换为空字符串

基准测试比最快的@johnchase解决方案慢,但我认为它更具可读性,并使用数据帧和序列的所有pandas方法:

^{pr2}$

相关问题 更多 >