How do I create a column in my dataframe called 'Yes Or No', where a '1' is input into each row where the "Date" column is between 01/01/2022 and 31/03/2022 and the "Datefield 2" column cannot be empty. I have started with the code below, but it doesn't produce the right output.
df['Yes Or No'] = (df['Datefield 2'] != [''] & pd.to_datetime(df['Date'], dayfirst=True).between(pd.Timestamp('2022-03-31'), pd.Timestamp('2023-01-01'))).astype(int)
CodePudding user response:
Convert both columns to datetimes and test:
d1 = pd.to_datetime(df['Date'], dayfirst=True)
d2 = pd.to_datetime(df['Datefield 2'], dayfirst=True)
df['Yes Or No'] = (d2.notna() & d1.between('2022-03-31', '2023-01-01')).astype(int)
CodePudding user response:
you can use numpy.where():
import numpy as np
df['Date']=pd.to_datetime(df['Date'])
df['yes_or_no']=np.where((df['Date'] >= '01/01/2022') & (df['Date'] <= '31/03/2022') & (df['Date2']!= ''),1,0)