Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update existing data frame in pandas?

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
like image 430
HappyPy Avatar asked Sep 10 '13 18:09

HappyPy


People also ask

How do I update DataFrame data in Python?

Python replace() method to update values in a dataframe Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it. As seen above, we have replaced the word “Siri” with “Code” within the dataframe.


1 Answers

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
like image 190
elyase Avatar answered Sep 25 '22 22:09

elyase