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

1

5

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 
Kulak answered 3/7, 2018 at 16:45 Comment(0)
P
4

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
Placatory answered 3/7, 2018 at 16:47 Comment(5)
Is it tested? When I ran this, I see a different outputPenetrate
` A B C 0 0 12 7 1 0 0 0 2 7 0 3 `Penetrate
@VenkataGogu df2 index start from 1 to 2(Not [0,1]) , I think you overlook itPlacatory
Hi @Wen, thanks for your answer, do you know how to do it if I have a multilevel index for example ('name','year', 'month', 'day')Kulak
pd.concat([df1,df2]).min(level=[0,1,2,3]) @KulakPlacatory

© 2022 - 2024 — McMap. All rights reserved.