how to make a dataframe by changing condition on url?

Question:

I have a dataframe, which contains many urls, is there a way i can make whenever it finds a specific url "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png" it gives a value False, else true?

My dataframe:

Expected:

surname value
First True
Second False
Third True
Fourth False
Asked By: Noob Coder

||

Answers:

Can you try the following:

url_pattern = "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"
df['value'] = df['image_url'].apply(lambda x: True if x == url_pattern else False)
Answered By: Jeril

You can use contains which creates a boolean mask. By assigning it to df['value'] you assign it to a new column, which gives you the desired result. This is even easier than creating a lambda function for it.

df['value'] = df.image_url.str.contains("https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png")
Answered By: Sandertjuhh