Marouan Sami
Marouan Sami

Reputation: 78

Is there a pandas function to evaluate if a substring is in a given string?

I'm filtering a Pandas Serie in order to find if the rows are substrings of a given string. I tried those instructions but couldn't succeed:

sting_to_test = "My String"
filtered_data = my_serie[my_serie in sting_to_test]

I also tested the isin() function but it seems that this can't be used in a single sting.

sting_to_test = "My String"
filtered_data = my_serie.isin(sting_to_test)

Is there any solution without iterating on the whole serie with a loop ?

Upvotes: 1

Views: 101

Answers (1)

niraj
niraj

Reputation: 18218

You can try using apply and lambda as following:

my_series = pd.Series(['AB', 'CD', 'BA'])
test_str = 'ABC'
print(my_series.apply(lambda row: row in test_str))

Upvotes: 1

Related Questions