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

Need to find and replace/correct list from another df column

I have a list let suppose F = [Jonii, Max, anna, xyz, etc..] and df which contains 2 column- Name and Corrected_Name.
df

I need to search each string from list into df[Name] and replace it with df[Corrected_Name]. For eg. in above, code will search list in df[Name] and if found which is "Jonii" then replace it with "Jon" which is from df[Corrected_Name].
So finally output will be f = [Jon, Max, anna, xyz, etc..]

Thanks in advance!! I am learner so pls ignore writing mistakes.

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

>Solution :

Maybe like this (Admittedly this is not the best way to do it):

import pandas as pd

df = pd.DataFrame({"Name": ["Johnii", "Tommi", "Marc"], "CorrectedName": ["John", "Tom", "Mark"]})
names = ["Johnii", "Elizabeth", "Arthur", "Tommi"]

output = []
for name in names:
    updated_value = [cn for n, cn in zip(df['Name'], df['CorrectedName']) if n == name]
    output.append(updated_value[0] if updated_value else name)

print(output)
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