2082. 富有客户的数量

发布时间 2023-08-13 22:04:48作者: 吾执青剑向天涯

2082. 富有客户的数量

2023年8月13日20:02:31

2082. 富有客户的数量

简单

SQL Schema


Pandas Schema


表: Store

+-------------+------+
| Column Name | Type |
+-------------+------+
| bill_id     | int  |
| customer_id | int  |
| amount      | int  |
+-------------+------+
bill_id 是这个表的主键(具有唯一值的列)。
每一行包含一个订单的金额及相关客户的信息。

编写解决方案找出 至少有一个 订单的金额 严格大于 500 的客户的数量。

返回结果格式如下示例所示:

示例 1:

输入:
Store 表:
+---------+-------------+--------+
| bill_id | customer_id | amount |
+---------+-------------+--------+
| 6       | 1           | 549    |
| 8       | 1           | 834    |
| 4       | 2           | 394    |
| 11      | 3           | 657    |
| 13      | 3           | 257    |
+---------+-------------+--------+
输出: 
+------------+
| rich_count |
+------------+
| 2          |
+------------+
解释:
客户 1 有 2 个订单金额严格大于 500。
客户 2 没有任何订单金额严格大于 500。
客户 3 有 1 个订单金额严格大于 500。

通过次数

3.8K

提交次数

5K

通过率

75.3%

答案

两种思路

1、先判断大于500的 然后对id去重 然后计数

2、用户id分组 取出最大值 判断大于500 然后计数

import pandas as pd

def count_rich_customers(store: pd.DataFrame) -> pd.DataFrame:
    # rich_customers=store[store['amount']>500]
    # count=rich_customers['customer_id'].nunique()
    # answer=pd.DataFrame({'rich_count':[count]})
    # return answer

    return pd.DataFrame({'rich_count':[store[store['amount']>500]['customer_id'].nunique()]})

    # return pd.DataFrame({"rich_count":[store.groupby("customer_id").apply(lambda x:x.max()>500)['amount'].sum()]})