'Lambda not in function doesn't work for more than one word in Python
I would like to filter dataframe by lambda if condition
I have a "product name" and "category1" columns and "if product name" not contains ("boxer","boxers","sock","socks") words I would like to change "category1" column as "Other", but below code change all of them as "other" example even contains "sock"
df = pd.DataFrame({
'product_name': ["blue shirt", " medium boxers", "red jackets ", "blue sock"],})
df["category1"]=df.apply(lambda x: "Other" if ("boxer","boxers","sock","socks" not in x["product_name"] ) else x["category1"], axis=1)
I expected below results
df = pd.DataFrame({
'product_name': ["blue shirt", " medium boxers", "red jackets ", "blue sock"],
'category1'["other", Nan, "other ", "Nan"],})
Thank you for your support
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
