1907. 按分类统计薪水

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

1907. 按分类统计薪水

2023年8月13日20:58:41

1907. 按分类统计薪水

中等

23

相关企业

SQL Schema


Pandas Schema


表: Accounts

+-------------+------+
| 列名        | 类型  |
+-------------+------+
| account_id  | int  |
| income      | int  |
+-------------+------+
在 SQL 中,account_id 是这个表的主键。
每一行都包含一个银行帐户的月收入的信息。

查询每个工资类别的银行账户数量。 工资类别如下:

  • "Low Salary":所有工资 严格低于 20000 美元。
  • "Average Salary"包含 范围内的所有工资 [$20000, $50000]
  • "High Salary":所有工资 严格大于 50000 美元。

结果表 必须 包含所有三个类别。 如果某个类别中没有帐户,则报告 0

任意顺序 返回结果表。

查询结果格式如下示例。

示例 1:

输入:
Accounts 表:
+------------+--------+
| account_id | income |
+------------+--------+
| 3          | 108939 |
| 2          | 12747  |
| 8          | 87709  |
| 6          | 91796  |
+------------+--------+
输出:
+----------------+----------------+
| category       | accounts_count |
+----------------+----------------+
| Low Salary     | 1              |
| Average Salary | 0              |
| High Salary    | 3              |
+----------------+----------------+
解释:
低薪: 数量为 2.
中等薪水: 没有.
高薪: 有三个账户,他们是 3, 6和 8.

通过次数

6.6K

提交次数

10.2K

通过率

64.7%

答案

import pandas as pd

def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:
    accounts['category'] = pd.cut(accounts['income'],bins=[-1,19999,50000,9999999],labels = ["Low Salary","Average Salary","High Salary"])
    accounts =  accounts.groupby('category').count() # .apply(lambda x:x.count())

    accounts['category'] =accounts.index
    return accounts.rename(columns={"income":"accounts_count"})[["category","accounts_count"]]