I am trying to add a new column containing label with this condition:
- Label 1 if delta time between value in 'time' and dt < 2 hours
- Label 0 for other case
My current idea:
df = pd.read_csv('./datetimecek.csv')
df['time'] = pd.to_datetime(df['datetime'])
dt = datetime.strptime("19/02/18 19:00", "%d/%m/%y %H:%M")
datetime time
2018/02/19 16:00 2018-02-19 16:00:00
2018/02/19 17:00 2018-02-19 17:00:00
2018/02/19 18:00 2018-02-19 18:00:00
2018/02/19 19:00 2018-02-19 19:00:00
And then I defined timedelta
a = timedelta(hours=2)
def label(c):
if dt - df['time'] < a:
return '1'
else:
return '0'
then
df['label'] = df.apply(label, axis=1)
But I got error: 'The truth value of a Series is ambiguous. Use a.empty, a.bool()...
Is there anyway I can fix this?
cin the function definition oflabelinstead of the entiredfexisting in global scope.