Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas assign value to dataframe column based on two lists using one column of the dataframe as index

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading