- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
时分时间点分类时间段,统计每个时间段出现次数,结果放入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
pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能。有什么不同吗? 最佳答案 pivot_table没有 normalize争论,不幸的是。 在 crosstab
我能找到的最接近的答案似乎太复杂:How I can create an interval column in pandas? 如果我有一个如下所示的 pandas 数据框: +-------+ |
这是我用来将某一行的一列值移动到同一行的另一列的当前代码: #Move 2014/15 column ValB to column ValA df.loc[(df.Survey_year == 201
我有一个以下格式的 Pandas 数据框: df = pd.DataFrame({'a' : [0,1,2,3,4,5,6], 'b' : [-0.5, 0.0, 1.0, 1.2, 1.4,
所以我有这两个数据框,我想得到一个新的数据框,它由两个数据框的行的克罗内克积组成。正确的做法是什么? 举个例子:数据框1 c1 c2 0 10 100 1 11 110 2 12
TL;DR:在 pandas 中,如何绘制条形图以使其 x 轴刻度标签看起来像折线图? 我制作了一个间隔均匀的时间序列(每天一个项目),并且可以像这样很好地绘制它: intensity[350:450
我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1): Time1 Time2
从这个 df 去的正确方法是什么: >>> df=pd.DataFrame({'a':['jeff','bob','jill'], 'b':['bob','jeff','mike']}) >>> df
我想按周从 Pandas 框架中的列中累积计算唯一值。例如,假设我有这样的数据: df = pd.DataFrame({'user_id':[1,1,1,2,2,2],'week':[1,1,2,1,
数据透视表的表示形式看起来不像我在寻找的东西,更具体地说,结果行的顺序。 我不知道如何以正确的方式进行更改。 df示例: test_df = pd.DataFrame({'name':['name_1
我有一个数据框,如下所示。 Category Actual Predicted 1 1 1 1 0
我有一个 df,如下所示。 df: ID open_date limit 1 2020-06-03 100 1 2020-06-23 500
我有一个 df ,其中包含与唯一值关联的各种字符串。对于这些唯一值,我想删除不等于单独列表的行,最后一行除外。 下面使用 Label 中的各种字符串值与 Item 相关联.所以对于每个唯一的 Item
考虑以下具有相同名称的列的数据框(显然,这确实发生了,目前我有一个像这样的数据集!:() >>> df = pd.DataFrame({"a":range(10,15),"b":range(5,10)
我在 Pandas 中有一个 DF,它看起来像: Letters Numbers A 1 A 3 A 2 A 1 B 1 B 2
如何减去两列之间的时间并将其转换为分钟 Date Time Ordered Time Delivered 0 1/11/19 9:25:00 am 10:58:00 am
我试图理解 pandas 中的下/上百分位数计算,但有点困惑。这是它的示例代码和输出。 test = pd.Series([7, 15, 36, 39, 40, 41]) test.describe(
我有一个多索引数据框,如下所示: TQ bought HT Detailed Instru
我需要从包含值“低”,“中”或“高”的数据框列创建直方图。当我尝试执行通常的df.column.hist()时,出现以下错误。 ex3.Severity.value_counts() Out[85]:
我试图根据另一列的长度对一列进行子串,但结果集是 NaN .我究竟做错了什么? import pandas as pd df = pd.DataFrame([['abcdefghi','xyz'],
我是一名优秀的程序员,十分优秀!