【问题标题】:Pandas dataset: get time spentPandas 数据集:获取花费的时间
【发布时间】:2021-02-05 17:26:09
【问题描述】:

我被困在一个我试图在 pandas 中解决的谜题上。通常我使用 Excel 以非常手动的方式修复此问题,但我想找到一种 pandas 方式。

我有一张包含工人开始和停止时间的表格(小摘录):

Name action date time
Adam in day1 08:00
Bert in day1 08:09
Chrissy in day1 09:00
Bert out day1 11:30
Adam out day1 12:00
Bert in day1 12:00
Chrissy out day1 18:00
Earl in day1 18:00
Earl out day1 23:59
Earl in day2 09:00
Bert in day2 09:01
Chrissy in day2 10:00
Bert out day2 10:11
Earl out day2 10:12
Bert in day2 10:15
Chrissy out day2 19:00

我正在尝试找出他们每个人每天花费多少时间...我正在通过双 for 循环中的迭代来解决这个问题,但我只是错过了 pandas 中内置的一些东西,因为我从来没有和熊猫一起经历这么多……

不寻找完整的解决方案,指针将不胜感激

【问题讨论】:

    标签: python pandas time


    【解决方案1】:

    类似的东西:

    # we work with timedelta type
    df['time'] = pd.to_timedelta(df['time']+':00')
    
    # we want to label the `in` and `out` in pairs
    df['no_action'] = df.sort_values('time').groupby(['Name','date','action']).cumcount()
    
    # pivot table so `in`, `out` pairs are on the same row
    actions = df.pivot(index=['Name','date', 'no_action'], columns='action', values='time')
    
    # differences between `in` and `out`
    actions['diff'] = actions['out'] - actions['in']
    
    # finally sum over `Name` and `date` then pivot
    actions['diff'].sum(level=['Name','date']).unstack('date')
    

    输出:

    date               day1            day2 
    Name                                    
    Adam     0 days 04:00:00             NaT
    Bert     0 days 03:21:00 0 days 01:10:00
    Chrissy  0 days 09:00:00 0 days 09:00:00
    Earl     0 days 05:59:00 0 days 01:12:00
    

    注意:这不包括跨越午夜的 inout 对。

    【讨论】:

      【解决方案2】:

      这是一个使用数据透视表的简单解决方案。

      df['time'] = pd.to_datetime(df['time'])  # defaults to today
      table = df.pivot_table(
          columns='action', values='time', index=['Name', 'date'], aggfunc='first'
      )
      
      >>> table
      action                        in                 out
      Name    date                                        
      Adam    day1 2021-02-05 08:00:00 2021-02-05 12:00:00
      Bert    day1 2021-02-05 08:09:00 2021-02-05 11:30:00
              day2 2021-02-05 09:01:00 2021-02-05 10:11:00
      Chrissy day1 2021-02-05 09:00:00 2021-02-05 18:00:00
              day2 2021-02-05 10:00:00 2021-02-05 19:00:00
      Earl    day1 2021-02-05 18:00:00 2021-02-05 23:59:00
              day2 2021-02-05 09:00:00 2021-02-05 10:12:00
      
      >>> table['out'] - table['in']
      Name     date
      Adam     day1   0 days 04:00:00
      Bert     day1   0 days 03:21:00
               day2   0 days 01:10:00
      Chrissy  day1   0 days 09:00:00
               day2   0 days 09:00:00
      Earl     day1   0 days 05:59:00
               day2   0 days 01:12:00
      dtype: timedelta64[ns]
      

      【讨论】:

        【解决方案3】:

        如果time 列不是时间序列,首先需要将其转换为时间戳,如下所示:

        df['time'] = pd.to_datetime(df['time'], format='%H:%M')
        
        # Now you just use groupby on Name and date column
        person_time_spent = df.groupby(['Name', 'date'])['time'].apply(lambda x: x.max() - x.min())
        

        【讨论】:

          猜你喜欢
          • 2016-10-08
          • 1970-01-01
          • 2019-07-14
          • 1970-01-01
          • 1970-01-01
          • 2017-11-17
          • 2023-04-04
          • 2012-04-04
          • 1970-01-01
          相关资源
          最近更新 更多