Home > Mobile >  how to make a dataframe by changing condition on url?
how to make a dataframe by changing condition on url?

Time:08-22

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:

surname image_url
First https://pbs.twimg.com/profile_images/1557569949756899328/L4llDRNh_normal.jpg|
Second https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png|
Third https://pbs.twimg.com/profile_images/1546820730616258560/wQfm-Ypn_normal.jpg
Fourth https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png

Expected:

surname value
First True
Second False
Third True
Fourth False

CodePudding user response:

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")

CodePudding user response:

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)
  • Related