I have these two dataframes: (update: I've added one column stuffto df1 to specify that the two dataframes have not the same schemas)
df1 = pd.DataFrame({'id': ['1','2','3'],
'val': [0, 0, 0],
'stuff': ['foo', 'bar', 'spam']})
df2 = pd.DataFrame({'id': ['2','3'], 'val': [10, 20]})
print(df1)
id val stuff
0 1 0 foo
1 2 0 bar
2 3 0 spam
print(df2)
id val
0 2 10
1 3 20
I want to update the values in df1 val column with the values from df2 val column based on the id column. Desired result after transformation on df1:
print(df1)
id val stuff
0 1 0 foo
1 2 10 bar
2 3 20 spam
I could use a join (merge) but then I would need several more steps to end up with the expected result (casting the column from float to int, dropping column, etc.). (BTW, if you have a simple and elegant way to it with a join, I am also interested).
I am trying to use slicing methods but could not figure out how. Example:
>>> df1.loc[df1['id'].isin(df2['id']), 'val'] = df2['val']
gives:
print(df1)
id val stuff
0 1 0.0 foo
1 2 20.0 bar
2 3 NaN spam
Update: One more constraint: do not modify the original df1 index.