I have the following lists: brands_list = {"b1": {"name": "brand1"}, "b2". {"name": "brand2"}} and actual_brands = ["brand1"], and a Pandas dataframe with a column brand with the following content: "b1", "b1", "b1", "b2", "b1", and I want to assign a value to column is_brand_present if the element of brands_list with index of column brand is in actual_brands.
I try the following using numpy’s where:
brands_list = {"b1": "brand1", "b2". "brand2"}
actual_brands = ["brand1"]
data_frame["is_brand_present"] = np.where(
brands_list[data_frame["brand"]].isin(actual_brands), 1, 0
)
I expect the content of column is_brand_present to be 1,1,1,0,1, but I’m getting this error:
TypeError: unhashable type: 'Series'
How can I make the evaluation of the condition?
>Solution :
We can just do
l = [x for x, y in brands_list.items() if y['name'] in actual_brands ]
df['is_brand_present'] = df.brand.isin(l).astype(int)