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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With