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

纵饮孤独 提交于 2021-02-10 04:31:19

问题


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 

回答1:


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


来源:https://stackoverflow.com/questions/51159478/create-a-data-frame-based-on-the-minimum-value-of-two-data-frames-pandas-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!