Home > Software design >  fastest way to replace values in one df with values from another df
fastest way to replace values in one df with values from another df

Time:01-09

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 :

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?

CodePudding user response:

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']))
  • Related