← 返回题库
初级

统计员工餐费账户余额分布

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd
    accts=pd.read_csv(StringIO(open_url("https://data.zuihe.com/dbd/ms-mealhub/state_05/meal_accounts.csv").read()))
    latest=accts.groupby('employee_id')['balance_after'].last().reset_index(name='balance')
    print(f"Total accounts: {len(latest)}")
    print(f"Total balance: {round(latest['balance'].sum(),2)}")
    print(f"Avg balance: {round(latest['balance'].mean(),2)}")
    print(f"Negative balance: {len(latest[latest['balance']<0])}")
    bins=[float('-inf'),0,50,100,200,float('inf')]; labels=['<0','0-50','50-100','100-200','>200']
    latest['range']=pd.cut(latest['balance'],bins=bins,labels=labels)
    dist=dict(latest.groupby('range',observed=True).size())
    print("Balance distribution:")
    for r,c in dist.items(): print(f"  {r}: {c} ({round(c/len(latest)*100,1)}%)")

示例

输入
solve()
期望输出
Total accounts: 80
Total balance: 20131.77
Avg balance: 251.65
Negative balance: 0
Balance distribution:
  0-50: 10 (12.5%)
  50-100: 7 (8.8%)
  100-200: 15 (18.8%)
  >200: 48 (60.0%)
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码