Python 比较表的列并将结果放入另一个表中
我有一个数据框,看起来像这样:Python 比较表的列并将结果放入另一个表中,python,pandas,dataframe,metadata,Python,Pandas,Dataframe,Metadata,我有一个数据框,看起来像这样: Date Input Val1 Val2 1-Dec X 10 5 2-Dec Y 15 3-Dec Z 4 5 4-Dec A 10 我希望输出的方式是,如果Val1出现在输出中,那么Val1将
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
我对pd.Concat进行了处理,但没有给出正确的输出最简单的是使用:
测试缺失值的解决方案是:
你可以查看这个链接
df['Output'] = df['Val1'].fillna(df['Val2'])
df['Output'] = np.where(df['Val1'].isna(), df['Val2'], df['Val1'])