How to replace&add the dataframe element by another dataframe in Python Pandas?

Posted by bigbug on Stack Overflow See other posts from Stack Overflow or by bigbug
Published on 2012-08-31T15:00:12Z Indexed on 2012/08/31 15:39 UTC
Read the original article Hit count: 205

Filed under:
|

Suppose I have two data frame 'df_a' & 'df_b' , both have the same index structure and columns, but some of the inside data elements are different:

>>> df_a
           sales cogs
STK_ID QT           
000876 1   100  100
       2   100  100
       3   100  100
       4   100  100
       5   100  100
       6   100  100
       7   100  100

>>> df_b
           sales cogs
STK_ID QT           
000876 5    50   50
       6    50   50
       7    50   50
       8    50   50
       9    50   50
       10   50   50

And now I want to replace the element of df_a by element of df_b which have the same (index, column) coordinate, and attach df_b's elements whose (index, column) coordinate beyond the scope of df_a . Just like add a patch 'df_b' to 'df_a' :

>>> df_c = patch(df_a,df_b)
           sales cogs
STK_ID QT           
000876 1   100  100
       2   100  100
       3   100  100
       4   100  100
       5    50   50
       6    50   50
       7    50   50
       8    50   50
       9    50   50
       10   50   50

How to write the 'patch(df_a,df_b)' function ?

© Stack Overflow or respective owner

Related posts about python

Related posts about pandas