Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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 ?

like image 659
bigbug Avatar asked Aug 31 '12 15:08

bigbug


3 Answers

Try this:

df_c = df_a.reindex(df_a.index | df_b.index)
df_c.ix[df_b.index] = df_b
like image 161
BrenBarn Avatar answered Sep 23 '22 14:09

BrenBarn


To fill gaps in one dataframe with values (or even full rows) from another, take a look at the df.combine_first() built-in method.

In [34]: df_b.combine_first(df_a)
Out[34]: 
           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
like image 23
Garrett Avatar answered Sep 21 '22 14:09

Garrett


Similar to BrenBarn's answer, but with more flexibility:

# reindex both to union of indices
df_ar = df_a.reindex(df_a.index | df_b.index)
df_br = df_b.reindex(df_a.index | df_b.index)

# replacement criteria can be put in this lambda function
combiner = lambda: x, y: np.where(y < x, y, x)
df_c = df_ar.combine(df.br, combiner)
like image 44
Def_Os Avatar answered Sep 23 '22 14:09

Def_Os