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

Set dataframe value based on condition of another dataframe

Table 1:

ID Name Column C Column D
1234hj Bob 1 1
nkj234 Joe 2 2
ji3251 Schmoe 3 3

Table 2:

ID Name
Bob
Joe
Sam

I currently have 2 dataframes like so. How do i extract the ID from table 1 and set it as ID in table 2 IF the name matches?

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

I’ve tried this code but requires same labelling. (This may not even be correct)

df2['ID'] = np.where(df['Name'] == df2['Name'], 
                        df['Prompt'], df2['ID'])

>Solution :

You can try this:

df2['ID'] = df2['Name'].map(df.set_index('Name')['ID'])

pd.merge is also an option here.

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