比较 table 的列并将结果放入另一个 table

Compare the column of table and put result in another table

我有一个如下所示的数据框:

Date      Input         Val1        Val2
1-Dec      X             10           5
2-Dec      Y                          15
3-Dec      Z             4            5
4-Dec      A                          10

我希望以这种方式输出,如果 Val1 存在,则在输出中显示 val1,否则将在输出中显示 Val2,所以我的输出将是:

Date      Input         Val1        Val2            Output
1-Dec      X             10           5              10           
2-Dec      Y                          15             15            since Val1 is missing so took Val2 in output
3-Dec      Z             4            5              4  
4-Dec      A                          10             10            since Val1 is missing so took Val2 in output

我 tred pd.Concat 但它没有给出正确的输出

最简单的就是使用Series.fillna:

df['Output'] = df['Val1'].fillna(df['Val2'])

测试缺失值的解决方案是numpy.where

df['Output'] = np.where(df['Val1'].isna(), df['Val2'], df['Val1'])