Search code examples
pythonpandasminimum

create a data frame based on the minimum value of two data frames pandas python


I have two data frames with different sizes. I want to replace the values of the first data frame by the values of the second data frame only if the values of the second data frame are less than the values of the first data frame. In other words I want to find the minimum values of the two data frames for each position for matching indices of the two dataframes.

df1:

      A     B     C   
0     0     12    7  
1     15    20    0  
2     7     0     3  

df2:

      A     B     C   
1     4     25    8  
2     0     0     5  

result df:

      A     B     C   
0     0     12    7  
1     4     20    0  
2     0     0     3 

Solution

  • Use:

    pd.concat([df1,df2]).min(level=0)
    Out[492]: 
       A   B  C
    0  0  12  7
    1  4  20  0
    2  0   0  3