简体   繁体   English

基于两个数据帧pandas python的最小值创建数据帧

[英]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: DF1:

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

df2: DF2:

      A     B     C   
1     4     25    8  
2     0     0     5  

result df: 结果df:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM