Home > OS >  Python Dataframe For loop (if i.contains)
Python Dataframe For loop (if i.contains)

Time:10-08

I need a for loop on a column specific. With this for loop, I will assign the value 'Normal' to a list if the column contains 'Themes'. But I don't know how to write it here. I would be glad if you help. Thanks in advance :) Dataset

For loops that I try

CodePudding user response:

You can loop via apply-lambda too;

df["group"] = df["Developer Id"].apply(lambda x: "Normal" if "themes" in str(x).lower() else "Premium")

CodePudding user response:

You can use np.where which is basically an if-else statement:

df['group'] = np.where(df['Developer ID'].str.contains('Themes'), 'Normal', 'Premium')
  • Related