xxxvinxxx xxxvinxxx - 2 months ago 9
Python Question

Pandas and apply function to match a string

I have a df column containing various links, some of them containing the string

"search"
.

I want to create a function that - being applied to the column - returns a column containing
"search"
or
"other"
.

I write a function like:

search = 'search'
def page_type(x):
if x.str.contains(search):
return 'Search'
else:
return 'Other'

df['link'].apply(page_type)


but it gives me an error like:


AttributeError: 'unicode' object has no attribute 'str'


I guess I'm missing something when calling the str.contains().

Answer

.str applies to the whole Series but here you are dealing with the value inside the Series.

You can either do : df['link'].str.contains(search)
Or like you want : df['link'].apply(lambda x: 'Search' if search in x else 'Other')

Edit

More generic way:

def my_filter(x, val, c_1, c_2):
    return c_1 if val in x else  c_2 

df['link'].apply(lambda x: my_filter(x, 'homepage', 'homepage', 'other'))