Hiya so I have a data frame which has the time something occurs in one column and the time that it ends in the next column. I need to try and find the time difference between the two, but theyre both strings so it wont simply let me compare them, is there a way I can change them to ints (theyre in the format HH:MM:SS) I found a way to split them using .split (I’ve put what I did for the original time below, the I could do the same for the second column and work them out from there, but I was wondering if there was an easier way?
…
TIA!
JavaScript
x
13
13
1
q = 0
2
for int in range(long):
3
intel = df_data_bad_time1.loc[q,'Time']
4
H_M_S = intel.split(':')
5
df_data_bad_time1.loc[q,'Hours'] = H_M_S[0]
6
df_data_bad_time1.loc[q,'Mins'] = H_M_S[1]
7
df_data_bad_time1.loc[q,'Secs'] = H_M_S[2]
8
q = q + 1
9
df_data_bad_time1['Hours'] = pd.to_numeric(df_data_bad_time1['Hours'], errors='coerce').astype('Int64')
10
df_data_bad_time1['Mins'] = pd.to_numeric(df_data_bad_time1['Mins'], errors='coerce').astype('Int64')
11
df_data_bad_time1['Secs'] = pd.to_numeric(df_data_bad_time1['Secs'], errors='coerce').astype('Int64')
12
df_data_bad_time1.head(15)
13
Advertisement
Answer
Here’s a simple function I wrote, you can take a look at it and tell me if you don’t understand anything: https://repl.it/@d4nieldev/subtract-time