【问题标题】:Count of rows at fixed time intervals and associated percentages固定时间间隔的行数和相关百分比
【发布时间】:2022-12-07 23:05:06
【问题描述】:

我在 python 中有以下 pandas DataFrame:

id entry_time other_columns
1 16:02:04 other_values
2 15:02:04 other_values
3 10:32:04 other_values
4 21:22:44 other_values
5 09:02:04 other_values
6 11:02:04 other_values

给定以下时间间隔,我想根据进入时间计算前一个数据帧的行数。此外,我想获得一个包含结果 count 列的百分比(小数点后两位)的列。

time_slot count percentage
00:00-03:00 0 0.00
03:00-06:00 0 0.00
06:00-09:00 0 0.00
09:00-12:00 3 50.00
12:00-15:00 0 0.00
15:00-18:00 2 33.33
18:00-21:00 0 0.00
21:00-00:00 1 16.67

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    例子

     data = {'id': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6},
     'entry_time': {0: '16:02:04',
      1: '15:02:04',
      2: '10:32:04',
      3: '21:22:44',
      4: '09:02:04',
      5: '11:02:04'},
     'other_columns': {0: 'other_values',
      1: 'other_values',
      2: 'other_values',
      3: 'other_values',
      4: 'other_values',
      5: 'other_values'}}
    df = pd.DataFrame(data)
    

    df

        id  entry_time  other_columns
    0   1   16:02:04    other_values
    1   2   15:02:04    other_values
    2   3   10:32:04    other_values
    3   4   21:22:44    other_values
    4   5   09:02:04    other_values
    5   6   11:02:04    other_values
    

    代码

    使用pd.cut

    bins = range(0, 25, 3)
    labels = ['{}:00 - {}:00'.format(str(i).zfill(2), str(i + 3).zfill(2)) for i in bins[:-1]]
    s = pd.cut(pd.to_datetime(df['entry_time']).dt.hour, bins=bins, labels=labels, right=False)
    pd.concat([s.value_counts(), s.value_counts(normalize=True).mul(100)], keys=['count', 'percentage'], axis=1).reindex(labels).rename_axis('timeslot')
    

    结果:

    timeslot     count  percentage
    00:00 - 03:00   0   0.0
    03:00 - 06:00   0   0.0
    06:00 - 09:00   0   0.0
    09:00 - 12:00   3   50.0
    12:00 - 15:00   0   0.0
    15:00 - 18:00   2   33.3
    18:00 - 21:00   0   0.0
    21:00 - 24:00   1   16.7
    



    另一种方式

    s = pd.to_datetime(df['entry_time']).dt.hour.floordiv(3)
    df1 = pd.concat([s.value_counts(), s.value_counts(normalize=True).mul(100)], keys=['count', 'percentage'], axis=1).reindex(range(8)).fillna(0)
    df1.index = df1.index.map(lambda i:'{}:00 - {}:00'.format(str(i * 3).zfill(2), str(i * 3 + 3).zfill(2)))
    df1.rename_axis('timeslot')
    

    结果:

                 count  percentage
    timeslot        
    00:00 - 03:00   0.0 0.0
    03:00 - 06:00   0.0 0.0
    06:00 - 09:00   0.0 0.0
    09:00 - 12:00   3.0 50.0
    12:00 - 15:00   0.0 0.0
    15:00 - 18:00   2.0 33.3
    18:00 - 21:00   0.0 0.0
    21:00 - 24:00   1.0 16.7
    

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 2019-07-16
      • 2019-09-17
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      相关资源
      最近更新 更多