There are 2 solutions in Pandas
and 1 solution in SQL
.
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
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
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
According to my test results, No.2 is worse than No.1, both at time and memory, from my personal perspective.
# 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