如何用Python Pandas中的另一个数据帧替换和添加dataframe元素?

big*_*bug 5 python pandas

假设我有两个数据框'df_a'和'df_b',它们都具有相同的索引结构和列,但是一些内部数据元素是不同的:

>>> 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
Run Code Online (Sandbox Code Playgroud)

现在我想用df_b的元素替换具有相同(索引,列)坐标的df_a元素,并附加df_b的元素,其(索引,列)坐标超出df_a的范围.就像在'df_a'中添加补丁'df_b'一样:

>>> 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
Run Code Online (Sandbox Code Playgroud)

如何编写'patch(df_a,df_b)'函数?

Def*_*_Os 1

与 BrenBarn 的答案类似,但更灵活:

# 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)
Run Code Online (Sandbox Code Playgroud)