【发布时间】:2014-09-05 01:31:51
【问题描述】:
我想检查名为 strings 的系列中的单词是否以 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])
我想出了下面的代码,但是有没有更快的,或者更多熊猫风格的方法来做到这一点?
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)
【问题讨论】:
标签: python performance pandas