File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
2057-count-salary-categories Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ <p >Table: <code >Accounts</code ></p >
2+
3+ <pre >
4+ +-------------+------+
5+ | Column Name | Type |
6+ +-------------+------+
7+ | account_id | int |
8+ | income | int |
9+ +-------------+------+
10+ account_id is the primary key (column with unique values) for this table.
11+ Each row contains information about the monthly income for one bank account.
12+ </pre >
13+
14+ <p >  ; </p >
15+
16+ <p >Write a solution  ; to calculate the number of bank accounts for each salary category. The salary categories are:</p >
17+
18+ <ul >
19+ <li><code>"Low Salary"</code>: All the salaries <strong>strictly less</strong> than <code>$20000</code>.</li>
20+ <li><code>"Average Salary"</code>: All the salaries in the <strong>inclusive</strong> range <code>[$20000, $50000]</code>.</li>
21+ <li><code>"High Salary"</code>: All the salaries <strong>strictly greater</strong> than <code>$50000</code>.</li>
22+ </ul >
23+
24+ <p >The result table <strong >must</strong > contain all three categories. If there are no accounts in a category,  ; return  ; <code >0</code >.</p >
25+
26+ <p >Return the result table in <strong >any order</strong >.</p >
27+
28+ <p >The  ; result format is in the following example.</p >
29+
30+ <p >  ; </p >
31+ <p ><strong class =" example " >Example 1:</strong ></p >
32+
33+ <pre >
34+ <strong >Input:</strong >
35+ Accounts table:
36+ +------------+--------+
37+ | account_id | income |
38+ +------------+--------+
39+ | 3 | 108939 |
40+ | 2 | 12747 |
41+ | 8 | 87709 |
42+ | 6 | 91796 |
43+ +------------+--------+
44+ <strong >Output:</strong >
45+ +----------------+----------------+
46+ | category | accounts_count |
47+ +----------------+----------------+
48+ | Low Salary | 1 |
49+ | Average Salary | 0 |
50+ | High Salary | 3 |
51+ +----------------+----------------+
52+ <strong >Explanation:</strong >
53+ Low Salary: Account 2.
54+ Average Salary: No accounts.
55+ High Salary: Accounts 3, 6, and 8.
56+ </pre >
Original file line number Diff line number Diff line change 1+ import pandas as pd
2+
3+ def count_salary_categories (accounts : pd .DataFrame ) -> pd .DataFrame :
4+ low_count = (accounts ['income' ] < 20000 ).sum ()
5+ average_count = ((accounts ['income' ] >= 20000 ) & (accounts ['income' ] <= 50000 )).sum ()
6+ high_count = (accounts ['income' ] > 50000 ).sum ()
7+
8+ ans = pd .DataFrame ({
9+ 'category' : ['Low Salary' , 'Average Salary' , 'High Salary' ],
10+ 'accounts_count' : [low_count , average_count , high_count ]
11+ })
12+
13+ return ans
You can’t perform that action at this time.
0 commit comments