配送表: Delivery
+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ delivery_id 是表的主键(具有唯一值的列)。 该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。
如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。
编写解决方案统计即时订单所占的百分比, 保留两位小数。
返回结果如下所示。
示例 1:
输入: Delivery 表: +-------------+-------------+------------+-----------------------------+ | delivery_id | customer_id | order_date | customer_pref_delivery_date | +-------------+-------------+------------+-----------------------------+ | 1 | 1 | 2019-08-01 | 2019-08-02 | | 2 | 5 | 2019-08-02 | 2019-08-02 | | 3 | 1 | 2019-08-11 | 2019-08-11 | | 4 | 3 | 2019-08-24 | 2019-08-26 | | 5 | 4 | 2019-08-21 | 2019-08-22 | | 6 | 2 | 2019-08-11 | 2019-08-13 | +-------------+-------------+------------+-----------------------------+ 输出: +----------------------+ | immediate_percentage | +----------------------+ | 33.33 | +----------------------+ 解释:2 和 3 号订单为即时订单,其他的为计划订单。
解答:
- import pandas as pd
-
- # 创建示例数据
- data = {
- "delivery_id": [1, 2, 3, 4, 5, 6],
- "customer_id": [1, 5, 1, 3, 4, 2],
- "order_date": ["2019-08-01", "2019-08-02", "2019-08-11", "2019-08-24", "2019-08-21", "2019-08-11"],
- "customer_pref_delivery_date": ["2019-08-02", "2019-08-02", "2019-08-11", "2019-08-26", "2019-08-22", "2019-08-13"]
- }
-
- df = pd.DataFrame(data)
-
- # 假如data中的数据储存不是时间类型,需要转换;反之,不需要这部分代码
- df['order_date'] = pd.to_datetime(df['order_date'])
- df['customer_pre_delivery_date'] = pd.to_datetime(df['customer_pref_delivery_date'])
-
- # 计算即时账单
- immediate_orders = df[df['order_date'] == df['customer_pref_delivery_date']].shape[0]
-
- # 计算总订单数
- total_orders = df.shape[0]
-
- # 计算即时订单百分比
- immediate_percentage = (immediate_orders / total_orders) * 100
-
- # 创建结果DataFrame
- result = pd.DataFrame({'immediate_percentage': [round(immediate_percentage, 2)]})
-
- # 显示结果
- print(result)
这里用到了字符串转时间类型。可参考之前的博客:
2024.6.3