我有大约1.3米的字符串(代表用户在发送IT帮助热线时的需求)在Pandas df中。我还有一系列的29813个名字,我想从这些字符串中删除,这样我只剩下描述问题的单词。这是一个数据的小例子-它可以工作,但它花费的时间太长了。我正在寻找一种更有效的方法来实现这个结果:
输入:
List1 = ["George Lucas has a problem logging in",
"George Clooney is trying to download data into a spreadsheet",
"Bart Graham needs to logon to CRM urgently",
"Lucy Anne George needs to pull management reports"]
List2 = ["Access Team", "Microsoft Team", "Access Team", "Reporting Team"]
df = pd.DataFrame({"Team":List2, "Text":List1})
xwords = pd.Series(["George", "Lucas", "Clooney", "Lucy", "Anne", "Bart", "Graham"])
for word in range(len(xwords)):
df["Text"] = df["Text"].str.replace(xwords[word], "! ")
# Just using ! in the example so one can clearly see the result
输出:
^{pr2}$我已经试着寻找答案了一段时间:如果我因为缺乏经验而错过了某个地方,请温柔一点,让我知道!在
非常感谢:)
我建议将文本标记化,并使用一组名称:
根据字符串的不同,标记化需要比仅在空格上拆分更为精细。在
可能有一种熊猫特有的方法可以做到这一点,但我对此几乎没有经验;)
在熊猫.Series.str.replace可以将已编译的正则表达式作为模式
也许这会有帮助?不过,我对这么长的正则表达式没有经验。在
感谢Ciprian Tomiagă指点我到帖子Speed up millions of regex replacements in Python 3。Eric Duminil提供的选项,请参阅“如果您想要最快的解决方案,请使用此方法(使用set lookup)”,在Pandas环境中使用series(而不是list)同样有效—下面重复此问题的示例代码,在我的大数据集上,整个过程在2.54秒内完成!在
输入:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐