用一个数据框中的值替换另一个数据框中的值的最快方法是什么?

huangapple go评论66阅读模式
英文:

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

问题

我有一个名为df1的数据帧,看起来像这样:

    class     val
    12        1271
    12        1271
    34        142
    34        142

还有另一个名为df2的数据帧,看起来像这样:

    class  val
    12     123
    34     141
    69     667

将CorrectVal映射到df1的最快方法是什么,以便结果数据帧如下所示:

    class     val
    12        123
    12        123
    34        141
    34        141

理想情况下,我会使用df.merge将这两个数据帧连接起来,删除val字段,并将CorrectVal重命名为val,如下所示:

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

但这可能不是最快的方法,对吗?

英文:

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?

答案1

得分: 0

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']))
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 14:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定