Python 每个 userprofileid 的总小时数和分钟数总和
Sum total hours and minutes for each userprofileid with Python
我想为每个 userprofileid
获得 totalhours
和 totalminutes
的数量。
例如:
userprofileid totalhours totalminutes
453 7.0 420
120 7.5 450
453 8.0 480
我无法删除 userprofileid
因为每个 ID 都有自己的小时和分钟。
我试过了,但我得到了总小时数和分钟数,并将它们添加到每一行中。
for user in clocking_left["userprofileid"]:
clocking_left["user_minutes_total"] = clocking_left["totalminutes"].sum()
clocking_left["user_hours_total"] = clocking_left["hours"].sum()
您可以使用 group by 对值求和
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))
输出
userprofileid totalhours totalminutes
120 7.5 450
453 15.0 900
我想为每个 userprofileid
获得 totalhours
和 totalminutes
的数量。
例如:
userprofileid totalhours totalminutes
453 7.0 420
120 7.5 450
453 8.0 480
我无法删除 userprofileid
因为每个 ID 都有自己的小时和分钟。
我试过了,但我得到了总小时数和分钟数,并将它们添加到每一行中。
for user in clocking_left["userprofileid"]:
clocking_left["user_minutes_total"] = clocking_left["totalminutes"].sum()
clocking_left["user_hours_total"] = clocking_left["hours"].sum()
您可以使用 group by 对值求和
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))
输出
userprofileid totalhours totalminutes
120 7.5 450
453 15.0 900