【问题标题】:Pandas Groupby if datetime within 30 minutes of each otherPandas Groupby 如果日期时间在 30 分钟内
【发布时间】:2022-01-15 08:08:42
【问题描述】:

您好,我想知道是否有人能够帮助解决以下问题。

在下表中,如果日期时间在 30 分钟内,我想按位置、电视和日期时间对数据进行分组,并为其分配一个组号。如果位置和电视相同,但它们的时间戳不在 30 分钟内,则应为其分配不同的组号。下面的示例表

location    TV      datetime

UK     bake off    29/10/2021 21:20:00

UK     bake off    29/10/2021 21:20:00

UK     bake off    29/10/2021 21:40:00

UK     masterchef  29/10/2021 22:47:00

UK     masterchef  29/10/2021 23:05:00


The  result I want is the below


location    TV      datetime           group

UK     bake off    29/10/2021 21:20:00  1

UK     bake off    29/10/2021 21:20:00  1

UK     bake off    29/10/2021 21:40:00  1

UK     masterchef  29/10/2021 22:47:00  2

UK     masterchef  29/10/2021 23:05:00  2

我使用 pd.Grouper 得到的结果如下。但是,由于 pd.Grouper 采用等间隔的时间间隔,因此它将第 3 行和第 5 行分组到不同的组中,如下所示。

location    TV     datetime           group

UK     bake off    29/10/2021 21:20:00  1

UK     bake off    29/10/2021 21:20:00  1

UK     bake off    29/10/2021 21:40:00  2

UK     masterchef  29/10/2021 22:47:00  3

UK     masterchef  29/10/2021 23:05:00  4

我正在使用以下代码:

df['group'] = df.groupby([pd.Grouper(key = 'datetime', freq = '30min'), 'location', 'TV']).ngroup()

【问题讨论】:

    标签: python pandas time pandas-groupby


    【解决方案1】:

    这是一种方法。它使用每个(位置、电视对)的最短时间。首先,设置数据:

    import pandas as pd
    
    # create data, with a new row
    data = [
        ('location', 'TV',      'datetime'),
        ('UK', 'bake off',    '29/10/2021 21:20:00'),
        ('UK', 'bake off',    '29/10/2021 21:20:00'),
        ('UK', 'bake off',    '29/10/2021 21:40:00'),
        ('UK', 'bake off',    '29/10/2021 23:59:59'),  # new
        ('UK', 'masterchef',  '29/10/2021 22:47:00'),
        ('UK', 'masterchef',  '29/10/2021 23:05:00'),
    ]
    

    然后,执行计算:

    df = pd.DataFrame(data = data[1:], columns = data[0])
    
    # convert to datetime
    df['datetime'] = pd.to_datetime(df['datetime'], format="%d/%m/%Y %H:%M:%S")
    
    # find min time for each (location, TV) pair
    df['start'] = df.groupby(['location', 'TV'])['datetime'].transform('min')
    
    # subtract min time, then integer division in 30-minute intervals
    df['elapsed'] = (df['datetime'] - df['start']).dt.seconds // (30 * 60)
    
    # assign group numbers
    df['group'] = df.groupby(['location', 'TV', 'elapsed']).ngroup()
    print(df)
    

    最后看看结果:

      location          TV            datetime               start  elapsed  group
    0       UK    bake off 2021-10-29 21:20:00 2021-10-29 21:20:00        0      0
    1       UK    bake off 2021-10-29 21:20:00 2021-10-29 21:20:00        0      0
    2       UK    bake off 2021-10-29 21:40:00 2021-10-29 21:20:00        0      0
    3       UK    bake off 2021-10-29 23:59:59 2021-10-29 21:20:00        5      1
    4       UK  masterchef 2021-10-29 22:47:00 2021-10-29 22:47:00        0      2
    5       UK  masterchef 2021-10-29 23:05:00 2021-10-29 22:47:00        0      2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2017-03-17
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多