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().
I think you need
numpy.where
:Timings:
You can use also a
list comprehesion
if you want to find the word search within a link:Fo example:
The output:
Create function:
.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: