英文:
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']))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论