Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: fastest way to check if words in Series A endswith one word of Series B

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)
like image 698
seb835 Avatar asked Sep 04 '14 15:09

seb835


1 Answers

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
like image 72
DSM Avatar answered Sep 24 '22 06:09

DSM