I need to calculate the time difference in minutes and create a separate entry in the data frame in minutes
I tried and got the following error
I need to calculate the time difference in minutes and create a separate entry in the data frame in minutes
I tried and got the following error
start_time = pd.to_datetime(df['Start time'].astype(str))
end_time = pd.to_datetime(df['End time'].astype(str))
df['diff'] = end_time.sub(start_time).dt.total_seconds().div(60)
I have stored values in seperate variable start_time and end_time
to do not override dataframe (i.e. Without convering datatype of columns to datetime
). Otherwise you can directly use it without saving in variables.
Use pd.to_datetime
:
df = pd.DataFrame({'Start time': ['07:15:00'], 'End time': ['10:40:00']})
df['Diff time'] = pd.to_datetime(df['End time']) \
.sub(pd.to_datetime(df['Start time'])) \
.dt.total_seconds().div(60)
Output:
>>> df
Start time End time Diff time
0 07:15:00 10:40:00 205.0