I have a pandas data frame like this:
how can i calculate the SUM of total time per group? the time format is in HH:MI. for example for G1 we've total (01:30 02:30 05:30)=09:30. I've checked many solutions like separating each item via ":" by for loop or multiplying it *60 but no success.
CodePudding user response:
You can convert each value to Timedelta
than sum them:
df['SUM'] = pd.to_timedelta(df['HOURS'].str.split(',').explode() [':00']) \
.groupby(level=0).sum()
print(df)
# Output
GROUP HOURS SUM
0 G1 01:30,02:30,05:30 0 days 09:30:00
1 G2 02:30,05:30,07:30 0 days 15:30:00