我试着四处寻找,但找不到一个简单的方法,所以我希望你的专业知识能有所帮助。在
我有一个有两列的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]
但我不相信有比现在更快的解决方案
这是一个很长的一个班轮,但它应该满足你的需要
我能想出的最快解决方案是使用
^{pr2}$replace
,如另一个答案中所述:原始答案:
与您当前的解决方案相比:
这些方法可以得到与当前解决方案相同的答案
我想您应该使用string所具有的replace()方法,它比使用正则表达式快几个数量级(我刚刚在IPython中快速检查过):
如果在这之后需要进一步的速度改进,您应该研究一下numpy的向量化函数(但是我认为使用replace代替正则表达式的速度应该相当可观)。在
您可以使用^{} 方法和
regex
参数来完成,然后使用str.strip
:注意您需要将
notnull
传递给testing.NAME
,因为没有它,NaN
值也将被替换为空字符串基准测试比最快的@johnchase解决方案慢,但我认为它更具可读性,并使用数据帧和序列的所有pandas方法:
^{pr2}$相关问题 更多 >
编程相关推荐