使用Python计算每个userprofileid的总小时和分钟数

2024-10-02 00:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到我拥有的每个userprofileidtotalhourstotalminutes的数量。
例如:

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()

Tags: idfor数量时间lefttotalsum小时
1条回答
网友
1楼 · 发布于 2024-10-02 00:37:27

您可以使用“分组依据”并汇总这些值


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

相关问题 更多 >

    热门问题