pandas - 大熊猫中日期时间列的日期级别的groupby聚合
问题描述
我有一个如下所示的数据框。这是一个医生预约数据。
Doctor Appointment Show
A 2020-01-18 12:00:00 Yes
A 2020-01-18 12:30:00 Yes
A 2020-01-18 13:00:00 No
A 2020-01-18 13:30:00 Yes
B 2020-01-18 12:00:00 Yes
B 2020-01-18 12:30:00 Yes
B 2020-01-18 13:00:00 No
B 2020-01-18 13:30:00 Yes
B 2020-01-18 16:00:00 No
B 2020-01-18 16:30:00 Yes
A 2020-01-19 12:00:00 Yes
A 2020-01-19 12:30:00 Yes
A 2020-01-19 13:00:00 No
A 2020-01-19 13:30:00 Yes
A 2020-01-19 14:00:00 Yes
A 2020-01-19 14:30:00 No
A 2020-01-19 16:00:00 No
A 2020-01-19 16:30:00 Yes
B 2020-01-19 12:00:00 Yes
B 2020-01-19 12:30:00 Yes
B 2020-01-19 13:00:00 No
B 2020-01-19 13:30:00 Yes
B 2020-01-19 14:00:00 No
B 2020-01-19 14:30:00 Yes
B 2020-01-19 15:00:00 No
B 2020-01-18 15:30:00 Yes
从上面的数据框中,我想在 pandas 中创建一个函数,它将输出以下内容。
我在下面试过
def Doctor_date_summary(doctor, date):
Number of slots = df.groupby([doctor, date] ).sum()
预期输出:
Doctor_date_summary(Doctor, date)
If Doctor = A, date = 2020-01-19
Number of slots = 8
Number of show up = 5
show up percentage = 62.5
其中,该医生在该日期的显示列中是数 = 5
解决方案
您可以先从这里创建一个日期列:
df['day'] = df['Appointment'].dt.floor('d')
然后你可以使用布尔索引:
def Doctor_date_summary(Doctor, date):
number_of_show_up = np.sum((df['Doctor']==Doctor) & (df['day']==date) & (df['Show']=='Yes'))
number_of_slots = np.sum((df['Doctor']==Doctor) & (df['day']==date))
return number_of_show_up, number_of_slots, 100*number_of_show_up/number_of_slots
最后:
number_of_show_up, number_of_slots, percentage = Doctor_date_summary('A', '2020-01-19')
print("Number of slots = {}".format(number_of_slots))
print("Number of show up = {}".format(number_of_show_up))
print("show up percentage = {:.1f}".format(percentage))
Number of slots = 8
Number of show up = 5
show up percentage = 62.5
推荐阅读
- spring-boot - 具有 1 个密钥或多个密钥的 Kafka Streams 性能
- css - Bootstrap Navbar 与其他框架
- mysql - 如何根据其他两个表中定义的关系内部连接两个表
- android - android studio 模拟器不工作(无法将属性“qemu.gles”设置为“1”)
- php - 以指定模式对自定义帖子类型进行分页
- android - 用户成功注册时再创建两个用户 - Firebase
- python - MongoDB获取选择字段
- html - 如何在 safari 中删除渐变文本的上边框
- angular - 将 Angular 2.x 应用程序升级到 Angular 8.x 版本 - 需要对 Angular 更新指南警告进行一些思考
- c++ - C++ MSVC - 显示未捕获的异常消息