pandas :检查A系列单词是否以B系列单词结尾的最快方法 [英] Pandas: fastest way to check if words in Series A endswith one word of Series B
本文介绍了 pandas :检查A系列单词是否以B系列单词结尾的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查名为strings
的系列中的单词是否以系列ending_strings
的一个单词结尾.
I want to check if the words in a Series named strings
ends with one words of a Series ending_strings
.
strings = Series(['om', 'foo_nom', 'nom', 'bar_foo', 'foo','blah'])
ending_strings = Series(['nom', 'foo'])
expected_results = Series([False, True, True, True, True, False])
我想出了以下代码,但是有没有更快或更更多的熊猫风格方式?
I've come up with the following code, but is there a faster, or more pandas style way to do this?
from pandas import Series
def ew(v):
return strings.str.endswith(v)
result = ending_strings.apply(ew).apply(sum).astype(bool)
result.equals(expected_results)
推荐答案
您可以在此处传递endswith
元组(因此,最好使用它代替Series):
You can pass endswith
a tuple here (so you might as well use that instead of a Series):
>>> strings = Series(['om', 'foo_nom', 'nom', 'bar_foo', 'foo','blah'])
>>> ending_strings = ("nom", "foo")
>>> strings.str.endswith(ending_strings)
0 False
1 True
2 True
3 True
4 True
5 False
dtype: bool
这篇关于 pandas :检查A系列单词是否以B系列单词结尾的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文