0

I have a Dataframe which looks like this:

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

i want the output in such a way, that if Val1 is present in then in the output val1 will be diplayed otherwise it will take the Val2 in the output, so my output will be:

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

i tred pd.Concat but it is not giving correct output

SS25
  • 61
  • 7
  • 1
    You can check this link https://stackoverflow.com/questions/38152389/coalesce-values-from-2-columns-into-a-single-column-in-a-pandas-dataframe – Athar Nov 26 '19 at 07:32

1 Answers1

1

Simpliest is use Series.fillna:

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

Solution with testing missing values is numpy.where:

df['Output'] = np.where(df['Val1'].isna(), df['Val2'], df['Val1'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252