I want to create a new column that returns a value of 1 if the below condition is true and 2 if false and am not sure why the below isn't working?
t1 = x['timestamp_1'] < x['timestamp_2']
x['new'] = np.select([t1], [1], default=2)
use numpy where
#convert both columns to pd.to_datetime
x[['timestamp_1','timestamp_2']] = x[['timestamp_1','timestamp_2']].apply(pd.to_datetime,errors='coerce')
t1 = x['timestamp_1'] < x['timestamp_2']
x['new'] = np.where(t1,1,2)
which functions like:
np.where(cond,valueiftrue,valueiffalse)
Use np.where:
#Convert to datetime
x['timestamp_1'] = pd.to_datetime(x['timestamp_1'])
x['timestamp_2'] = pd.to_datetime(x['timestamp_2'])
t1 = x['timestamp_1'] < x['timestamp_2']
x['new'] = np.where(t1, 1, 2)
If the condition is true it will return 1 else it will return 2.
x['time1'] = pd.to_datetime(x['time1'] and similary for the other column
np.where(cond,valueiftrue,valueiffalse)pd.x['']a typo? should it bex['column']?