I have a larger dataframe in which there's a column that I would like to update with new values from a smaller dataframe only where row values for another column match. A left join has the correct rows but two columns of the values I want "updated" (left and right). I have tried a left merge with drops but of course that wipes out the values in the column I'm trying to update wherever the matching column doesn't match. I've also tried variations of this:
tips.loc[tips['tip'] < 2, 'tip'] = 2
but I get an error about series lengths needing to match because the table I'm updating from is smaller. I looked at DataFrame.combine_first() also but the docs don't show it takes any options and doesn't produce what I'm looking for either.
Here's an example:
df = pd.DataFrame({'CAT': ['A', 'B', 'C', 'D', 'E'], 'Value': [12, 34, np.NaN, 43, 21]})
CAT Value
0 A 12
1 B 34
2 C NaN
3 D 43
4 E 21
df2 = pd.DataFrame({'CAT': ['C', 'F', 'G'], 'Value': [22, 99, 44]})
CAT Value
0 C 22
1 F 99
2 G 44
the result should look like this:
CAT Value
0 A 12
1 B 34
2 C 22
3 D 43
4 E 21
Though the left frame in this example has NaN being updated, I'm not looking exclusively for this case. I want to overwrite whatever value is there. Any help? Thx.