1

I have dataframe like below:

    Date1     Date2     
 25-11-2020  23-11-2020       
             27-11-2020      
 26-11-2020                   
 29-11-2020  27-11-2020        
             29-11-2020       
 23-11-2020  27-11-2020  

I need to apply the below conditions on the dataframe:

  1. If Date1 and Date2 are present then Select Date 1
  2. Date 1 is null or not present select Date 2

Expected Output

Date1     Date2     Date_Select 
 25-11-2020  23-11-2020  25-11-2020      
             27-11-2020  27-11-2020      
 26-11-2020              26-11-2020      
 29-11-2020  27-11-2020  29-11-2020      
             29-11-2020  29-11-2020      
 23-11-2020  27-11-2020  23-11-2020     

How can this be in python?

1 Answers1

1

Try fillna:

df['Date1'].fillna(df['Date2'])

Or

df['Date1'].combine_first(df['Date2'])

Output:

0    25-11-2020
1    27-11-2020
2    26-11-2020
3    29-11-2020
4    29-11-2020
5    23-11-2020
Name: Date1, dtype: object
Scott Boston
  • 147,308
  • 15
  • 139
  • 187