我有一个看起来像这样的数据框:
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,但没有给出正确的输出
答案 0 :(得分:1)
最简单的是使用Series.fillna
:
df['Output'] = df['Val1'].fillna(df['Val2'])
测试缺失值的解决方案是numpy.where
:
df['Output'] = np.where(df['Val1'].isna(), df['Val2'], df['Val1'])