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

Replace column values based on a filter

Given this list

a = [a,d]

Give a dataset as such

id type
a cat
b fish
c dog
d cat

If the id items of the dataset are found in the list a, then I need to change the related type item, if not leave the same type item

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

End result

id type
a bird
b fish
c dog
d bird

This code is not working

df["type"] = np.where(df["id"].isin(a), df["type"].replace("bird"), df["type"])

>Solution :

Only set bird if match condition, replace is not necessary here if need set to same values:

df["type"] = np.where(df["id"].isin(a), "bird", df["type"])
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