5

Given these two data frames:

>>> df1 = pd.DataFrame({'c1':['a','a','b','b'], 'c2':['x','y','x','y'], 'val':0})
>>> df1
  c1 c2  val
0  a  x    0
1  a  y    0
2  b  x    0
3  b  y    0

>>> df2 = pd.DataFrame({'c1':['a','a','b'], 'c2':['x','y','y'], 'val':[12,31,14]})
>>> df2
  c1 c2  val
0  a  x   12
1  a  y   31
2  b  y   14

Is there a function that takes the elements of val from df2 and puts them in the corresponding indexes of df1, resulting in:

>>> df1_updated 
  c1 c2  val
0  a  x   12
1  a  y   31
2  b  x    0
3  b  y   14

1 Answer 1

8

Yes, take a look at combine_first or update. For example:

>>> df1['val'] = df2['val'].combine_first(df1['val'])
>>> df1
Out[26]:
    c1  c2  val
0    a   x   12
1    a   y   31
2    b   x   14
3    b   y   0

EDIT: to combine according to c1 and c2 ignoring the current index:

>>> df1['val'] = df2.set_index(['c1','c2'])['val'].combine_first(df1.set_index(['c1','c2'])['val']).values
>> df1
Out[25]:
    c1  c2  val
0    a   x   12
1    a   y   31
2    b   x   0
3    b   y   14
Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't work, because it doesn't take into account the df1 indexes. The value 14 should be in row b y
Great, many thanks! And thanks also for taking the time to solve this :)
This doesn't work for me. When I try: proxy.set_index(['symbol','symboltype'])['proxy'].combine_first(symbols.set_index(['symbol', 'symboltype'])['proxy']).value I get: Exception: cannot handle a non-unique multi-index!
@Yona, might be better if you create a mcve and post a new question. Might be an issue with your particular dataset or you might have found a bug in pandas!
The docs says combine_first ‘choosing the calling Series’s values first. Result index will be the union of the two indexes’. In this case, df1[‘val’] is not NaN nor null, so it won’t pick df2[‘val’]. i tried this on latest pandas and after comdbine_first df1[‘val’] is still 0. How come you have the output as shown?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.