python - 使用分类变量解包时间数据

标签 python pandas

我有以下数据框

        site    start_time  end_time    day_week    bike_repair car_repair  touring
0   a1  01/01/1901 11:00    01/01/1901 11:15    Sat_1   1   0   0
1   a2  01/01/1901 11:15    01/01/1901 11:30    Sat_1   0   0   1
2   a3  01/01/1901 11:30    01/01/1901 11:45    Sat_1   1   1   0
3   a4  01/01/1901 11:45    01/01/1901 12:00    Sat_1   1   0   0
4   a5  01/01/1901 12:00    01/01/1901 12:15    Sat_1   0   1   1
5   a6  01/01/1901 12:15    01/01/1901 12:30    Sat_1   1   1   1
6   a7  01/01/1901 12:30    01/01/1901 12:45    Sat_1   1   0   1
7   a8  01/01/1901 12:45    01/01/1901 13:00    Sat_2   0   1   0
8   a9  01/01/1901 13:00    01/01/1901 13:15    Sat_2   0   0   0
9   a10 01/01/1901 13:15    01/01/1901 13:30    Sat_2   1   1   1
10  a11 01/01/1901 13:30    01/01/1901 13:45    Sat_2   1   1   1
11  a12 01/01/1901 13:45    01/01/1901 14:00    Sat_2   1   0   1
12  a13 01/01/1901 14:00    01/01/1901 14:15    Sat_2   0   1   1
13  a14 01/01/1901 14:15    01/01/1901 14:30    Sat_2   0   1   1

请使用 pd.read_clipboard(sep='\t') 将 df 读入您的编辑器。

1 表示我在那 15 分钟的间隔内覆盖了该技能,0 表示我没有。

day_week 指的是星期几和周数。

现在,我想了解的是在任何给定时间我有什么承保范围。我有另一个数据框,它是我的网点的打开和关闭时间,我想在争论之后合并到它上面。

我已经从中融化了数据框,它给了我以下内容

(pd.melt(so,id_vars=['site','start_time','end_time','day_week']
,var_name='Skill',value_name='Coverage'))

        site    start_time  end_time    day_week    Skill   Coverage
0   a1  01/01/1901 11:00    01/01/1901 11:15    Sat_1   bike_repair 1
1   a2  01/01/1901 11:15    01/01/1901 11:30    Sat_1   bike_repair 0
2   a3  01/01/1901 11:30    01/01/1901 11:45    Sat_1   bike_repair 1
3   a4  01/01/1901 11:45    01/01/1901 12:00    Sat_1   bike_repair 1
4   a5  01/01/1901 12:00    01/01/1901 12:15    Sat_1   bike_repair 0
5   a6  01/01/1901 12:15    01/01/1901 12:30    Sat_1   bike_repair 1
6   a7  01/01/1901 12:30    01/01/1901 12:45    Sat_1   bike_repair 1
7   a8  01/01/1901 12:45    01/01/1901 13:00    Sat_2   bike_repair 0
8   a9  01/01/1901 13:00    01/01/1901 13:15    Sat_2   bike_repair 0
9   a10 01/01/1901 13:15    01/01/1901 13:30    Sat_2   bike_repair 1
10  a11 01/01/1901 13:30    01/01/1901 13:45    Sat_2   bike_repair 1

这让我接近我想要的,然后我想使用来自 this 的答案发布,但我遇到以下问题:

熔化后的数据是 6m 行,Skill 列中有 15 种不同的类型

在尝试调整答案后我得到一个内存错误,所以可以更改它以匹配 15 分钟的间隔并对覆盖率列求和吗?

我想得到类似的东西

site    time    day_week    skill   coverage
0   a1  11:00   Sat_1   bike_repair 1
1   a1  11:15   Sat_1   bike_repair 0
2   a1  11:30   Sat_1   bike_repair 1
3   a1  11:45   Sat_1   bike_repair 1
4   a1  12:00   Sat_1   bike_repair 0
5   a1  12:15   Sat_1   bike_repair 1

site    start_time  day_week    skill   Coverage
0   a1  11:01   Sat_1   bike_repair 1
1   a1  11:02   Sat_1   bike_repair 1
2   a1  11:03   Sat_1   bike_repair 1
3   a1  11:04   Sat_1   bike_repair 1
4   a1  11:05   Sat_1   bike_repair 1
5   a1  11:06   Sat_1   bike_repair 1
6   a1  11:07   Sat_1   bike_repair 1
7   a1  11:08   Sat_1   bike_repair 1
8   a1  11:09   Sat_1   bike_repair 1
9   a1  11:10   Sat_1   bike_repair 1
10  a1  11:11   Sat_1   bike_repair 1
11  a1  11:12   Sat_1   bike_repair 1
12  a1  11:13   Sat_1   bike_repair 1
13  a1  11:14   Sat_1   bike_repair 1
14  a1  11:15   Sat_1   bike_repair 0
15  a1  11:16   Sat_1   bike_repair 0
16  a1  11:17   Sat_1   bike_repair 0
17  a1  11:18   Sat_1   bike_repair 0
18  a1  11:19   Sat_1   bike_repair 0

最佳答案

我认为你需要:

so['start_time'] = pd.to_datetime(so['start_time'])
so['end_time'] = pd.to_datetime(so['end_time'])

df = (df.groupby(['site','Skill', df['start_time'].dt.time,'day_week'])['Coverage']
        .sum()
        .reset_index())

关于python - 使用分类变量解包时间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308242/

相关文章:

python - Matplotlib/v-3.4.2 matplotlibrc 中 latex 前导码的问题

python - 使用 pip freeze 忽略某些包及其依赖项

python - 如何以有效的方式将具有 MultiIndex 的数据帧合并到另一个数据帧中?

python - 如何在 pandas unstack 之前动态重命名列?

python - 如何将值与 csv 文件中的值进行比较?

python - 决策树中的递归编程

python - 基于组分配的 Pandas 数据帧到稀疏矩阵(如果在组中则为 1,如果不在组中则为 0)

datetime - 基于日期时间列对 pandas 数据框进行切片

python - 如何使用 distutils 安装到特定的 Python 安装?

python - 使用不同的数据框更改列值 - Python Pandas