Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 1.69 KB

1907. Count Salary Categories.md

File metadata and controls

57 lines (44 loc) · 1.69 KB

1907. Count Salary Categories

Question Link

There are 2 solutions in Pandas and 1 solution in SQL.

Pandas

Choice No.1: Directly create a DataFrame to output Choice No.2: Create a wide table and then use melt() turning it into a tall and narrow one

Code No.1

import pandas as pd

def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:

    low=accounts[accounts['income']<20000].shape[0]
    ave=accounts[(accounts['income']>=20000) & (accounts['income']<=50000)].shape[0]
    high=accounts[50000<accounts['income']].shape[0]

    c=pd.DataFrame({'category':['Low Salary','Average Salary','High Salary'],'accounts_count':[low,ave,high]})

    return c

Code No.2

import pandas as pd

def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame:

    low=accounts[accounts['income']<20000].shape[0]
    ave=accounts[(accounts['income']>=20000) & (accounts['income']<=50000)].shape[0]
    high=accounts[50000<accounts['income']].shape[0]

    c=pd.DataFrame({'Low Salary':[low],'Average Salary':[ave],'High Salary':[high]})
    c=c.melt(
        var_name='category',
        value_name='accounts_count'
    )

    return c

And...

According to my test results, No.2 is worse than No.1, both at time and memory, from my personal perspective.

SQL

# Write your MySQL query statement below
select "Low Salary" category, count(*) accounts_count from Accounts
where income<20000
union
select "Average Salary" category, count(*) accounts_count from Accounts
where income between 20000 and 50000
union
select "High Salary" category, count(*) accounts_count from Accounts
where income>50000