-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1907. Count Salary Categories
More file actions
28 lines (21 loc) · 1.17 KB
/
Copy path1907. Count Salary Categories
File metadata and controls
28 lines (21 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Table: Accounts
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id | int |
| income | int |
+-------------+------+
account_id is the primary key (column with unique values) for this table.
Each row contains information about the monthly income for one bank account.
Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:
"Low Salary": All the salaries strictly less than $20000.
"Average Salary": All the salaries in the inclusive range [$20000, $50000].
"High Salary": All the salaries strictly greater than $50000.
The result table must contain all three categories. If there are no accounts in a category, return 0.
Return the result table in any order.
import pandas as pd
def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:
bins = [-float('inf'), 19999, 50000, float('inf')]
labels = ['Low Salary', 'Average Salary', 'High Salary']
accounts['category'] = pd.cut(accounts['income'], bins=bins, labels=labels)
return accounts['category'].value_counts().reset_index().rename(columns={'count': 'accounts_count'}).sort_values(by='accounts_count', ascending=False)