Home > Software engineering >  Sum total hours and minutes for each userprofileid with Python
Sum total hours and minutes for each userprofileid with Python

Time:10-08

I want get the amount of totalhours and totalminutes for each userprofileid I have.
For example:

userprofileid      totalhours    totalminutes
453                7.0           420
120                7.5           450
453                8.0           480

I can't delete userprofileid because each id have their hours and minutes.
I tried this but I get total amount of hours and minutes, and add they in each row.

for user in clocking_left["userprofileid"]:
clocking_left["user_minutes_total"] = clocking_left["totalminutes"].sum()
clocking_left["user_hours_total"] = clocking_left["hours"].sum()

CodePudding user response:

You can use group by and sum up the values


import pandas as pd

data = {'userprofileid': [453,120,453],
        'totalhours': [7.0,7.5,8],
'totalminutes': [420,450,480]       
}

df = pd.DataFrame(data, columns = ['userprofileid','totalhours','totalminutes'])

df_new = df.groupby('userprofileid').sum().reset_index()

print(df_new.to_string(index=False))

output

userprofileid  totalhours  totalminutes
           120         7.5           450
           453        15.0           900
  • Related