一、每日一题
表: Orders
+-----------------+----------+ | Column Name | Type | +-----------------+----------+ | order_number | int | | customer_number | int | +-----------------+----------+ 在 SQL 中,Order_number是该表的主键。 此表包含关于订单ID和客户ID的信息。
查找下了 最多订单 的客户的 customer_number
。
测试用例生成后, 恰好有一个客户 比任何其他客户下了更多的订单。
查询结果格式如下所示。
示例 1:
输入: Orders 表: +--------------+-----------------+ | order_number | customer_number | +--------------+-----------------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 3 | +--------------+-----------------+ 输出: +-----------------+ | customer_number | +-----------------+ | 3 | +-----------------+ 解释: customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都要多,因为他们只有一个订单。 所以结果是该顾客的 customer_number ,也就是 3 。
import pandas as pd
def largest_orders(orders: pd.DataFrame) -> pd.DataFrame:
# 如果订单表为空,则返回一个包含'customer_number'列且没有行的空DataFrame
if len(orders) == 0:
return pd.DataFrame(None, columns=['customer_number'])
# 对customer_number进行分组,并计算每个组的订单数量
order_counts = orders.groupby('customer_number')['order_number'].count()
# 找到订单数量最多的customer_number
most_orders_customer_number = order_counts.idxmax()
# 创建一个包含最多订单客户的customer_number的DataFrame
result = pd.DataFrame({'customer_number': [most_orders_customer_number]})
# 返回结果DataFrame
return result
# 示例数据
data = [[1, 1], [2, 2], [3, 3], [4, 3]]
orders = pd.DataFrame(data, columns=['order_number', 'customer_number']).astype({'order_number': 'Int64', 'customer_number': 'Int64'})
# 调用函数并打印结果
result = largest_orders(orders)
print(result)
题源:Leetcode
二、总结
注意订单数为0的情况。
2024.6.10