时分时间点分类时间段,统计每个时间段出现次数,结果放入pandas数据帧,Python

x33g5p2x  于2022-07-26 转载在 Python  
字(2.0k)|赞(0)|评价(0)|浏览(596)

时分时间点分类时间段,统计每个时间段出现次数,结果放入pandas数据帧,Python

https://zhangphil.blog.csdn.net/article/details/125911971

https://zhangphil.blog.csdn.net/article/details/125911971

把不同的时间点划入对应的时间段区间,比如12:23,归入到(12,13)时间段,并计数1。15:23归入到(15,16)时间段,并计数1。如果出现新的时间点(比如12:51)落入到(12,13),增加计数,此时为2。把时间段和计数结果放入到pandas数据帧。

import datetime
import random
import pandas as pd

# 生成随机测试时间数量
from pprint import pprint

SAMPLE_COUNT = 10
SECTION = 'section'
SUM = 'sum'

def my_time():
    times = []
    for i in range(24):
        times.append({SECTION: (i, i + 1), SUM: 0})

    cnt = 0
    while True:
        h = random.randint(0, 23)
        m = random.randint(0, 59)
        t = datetime.time(hour=h, minute=m)
        for tx in times:
            if tx[SECTION][0] <= t.hour < tx[SECTION][1]:
                tx[SUM] = tx[SUM] + 1
                pprint(f'{t.strftime("%H:%M")} @ {tx[SECTION]}')
                break

        cnt = cnt + 1
        if cnt > SAMPLE_COUNT:
            break

    return times

if __name__ == '__main__':
    times = my_time()
    print('-')
    pprint(times)

    print('--')

    # 数据组装成pandas数据帧。
    pd_data = []
    for t in times:
        l = [t[SECTION], t[SUM]]
        pd_data.append(l)

    col = ['时间段', '时间点次数']
    df = pd.DataFrame(data=pd_data, columns=col)
    df = df.sort_values(by=col[1], axis=0, ascending=False)  # 降序

    # 重置索引
    df = df.reset_index(drop=True)
    df.index = df.index + 1

    pprint(df.head(10))

输出:

'22:28 @ (22, 23)'
'17:46 @ (17, 18)'
'13:17 @ (13, 14)'
'00:39 @ (0, 1)'
'00:25 @ (0, 1)'
'21:01 @ (21, 22)'
'10:31 @ (10, 11)'
'18:48 @ (18, 19)'
'19:00 @ (19, 20)'
'13:27 @ (13, 14)'
'19:37 @ (19, 20)'
-
[{'section': (0, 1), 'sum': 2},
 {'section': (1, 2), 'sum': 0},
 {'section': (2, 3), 'sum': 0},
 {'section': (3, 4), 'sum': 0},
 {'section': (4, 5), 'sum': 0},
 {'section': (5, 6), 'sum': 0},
 {'section': (6, 7), 'sum': 0},
 {'section': (7, 8), 'sum': 0},
 {'section': (8, 9), 'sum': 0},
 {'section': (9, 10), 'sum': 0},
 {'section': (10, 11), 'sum': 1},
 {'section': (11, 12), 'sum': 0},
 {'section': (12, 13), 'sum': 0},
 {'section': (13, 14), 'sum': 2},
 {'section': (14, 15), 'sum': 0},
 {'section': (15, 16), 'sum': 0},
 {'section': (16, 17), 'sum': 0},
 {'section': (17, 18), 'sum': 1},
 {'section': (18, 19), 'sum': 1},
 {'section': (19, 20), 'sum': 2},
 {'section': (20, 21), 'sum': 0},
 {'section': (21, 22), 'sum': 1},
 {'section': (22, 23), 'sum': 1},
 {'section': (23, 24), 'sum': 0}]
--
         时间段  时间点次数
1     (0, 1)      2
2   (13, 14)      2
3   (19, 20)      2
4   (22, 23)      1
5   (21, 22)      1
6   (18, 19)      1
7   (17, 18)      1
8   (10, 11)      1
9   (20, 21)      0
10  (16, 17)      0

相关文章