【问题标题】:DataFrame - Given a start date, end date , hours incurred daily, how do I go about calculating hours incurred for given month/year?DataFrame - 给定开始日期、结束日期、每天产生的小时数,我如何计算给定月/年产生的小时数?
【发布时间】:2021-07-08 00:47:56
【问题描述】:

我有一个由employee_id、start_date、end_date、hour_spent_per_day 组成的数据框。基本上对于每条记录,它都会记录员工每天花费的时间以及 start_Date 和 end_Date 给定的持续时间。

employee_id start_Date end_Date hours_spent_per_day
1234 2019-01-15 2019-01-15 2
1234 2019-01-15 2019-01-17 3
1236 2019-01-29 2019-02-28 4
1237 2019-01-19 2019-03-05 2
1237 2019-12-01 2020-01-10 2

我的目标是汇总每年每月花费的总小时数。 即

Year Month Hours Spent
2019 January 'hours spent for all the days in Jan-2019 for all employees
2019 February 'hours spent for all the days in Feb-2019 for all employees
2019 March 'hours spent for all the days in Mar-2019 for all employees
2019 December 'hours spent for all the days in Dec-2019 for all employees
2020 January 'hours spent for all the days in Jan-2020 for all employees

现在第二个表很容易(只是按 SQL 分组的问题)。但是我对第一个表有问题,即对于每一行,我如何确定每个月花费的时间(注意跨月是可能的)。我想我需要确定每一行所涵盖的确切月份/年份,然后乘以该月花费的时间。我被困住了,寻求您的帮助。

【问题讨论】:

    标签: python dataframe


    【解决方案1】:

    试试:

    df["start_Date"] = pd.to_datetime(df["start_Date"])
    df["end_Date"] = pd.to_datetime(df["end_Date"])
    
    df["tmp"] = df.apply(
        lambda x: pd.date_range(x["start_Date"], x["end_Date"]), axis=1
    )
    df = df.explode("tmp")
    
    x = df.groupby([df.tmp.dt.year, df.tmp.dt.month_name()])[
        "hours_spent_per_day"
    ].sum()
    
    x.index = x.index.rename(["Year", "Month"])
    print(x.reset_index().rename(columns={"hours_spent_per_day": "Hours Spent"}))
    

    打印:

       Year     Month  Hours Spent
    0  2019  December           62
    1  2019  February          168
    2  2019   January           49
    3  2019     March           10
    4  2020   January           20
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 2018-02-27
      • 2022-10-23
      相关资源
      最近更新 更多