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

fastest way to replace values in one df with values from another df

I have a dataframe df1 that looks like this :

class     val
12        1271
12        1271
34        142
34        142

and another df2 that looks like this

class  val
12     123
34     141
69     667

What would be the fastest way to map CorrectVal to df1 such that the resultant df is :

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

class     val
12        123
12        123
34        141
34        141

Ideally I would join the 2 dfs with df.merge and drop the val field and rename CorrectVal with val like so

df2 = df2.rename(columns={'val':'correctVal'})
df_resultant=df1.merge(df2, how ='left' , on='class')
df_resultant.drop(columns='val').rename(columns={'CorrectVal':'val'})

but this might not be the fastest way, right?

>Solution :

Your solution should be simlify with remove column val from df1:

df_resultant=df1.drop(columns='val').merge(df2, how ='left' , on='class')

Or use mapping by Series.map, I guess this solution should be faster, best test in real data:

df_resultant = df1.assign(val= df1['class'].map(df2.set_index('class')['val']))
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