← 返回题库
初级

解析小票条目并计算合计

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd
    items = pd.read_csv(StringIO(open_url("https://data.zuihe.com/dbd/ms-ledger/state_01/receipt_items.csv").read()))
    print(f"Total items: {len(items)}")
    by_rcp = items.groupby('receipt_id').agg(count=('item_id','count'),total=('subtotal','sum')).reset_index()
    by_rcp['total'] = by_rcp['total'].round(2)
    print(f"Receipts with items: {len(by_rcp)}")
    print(f"Avg items/receipt: {round(by_rcp['count'].mean(),1)}")
    for _, row in by_rcp.head(5).iterrows():
        print(f"  {row['receipt_id']}: {row['count']} items total={row['total']}")

示例

输入
solve()
期望输出
Total items: 345
Receipts with items: 200
Avg items/receipt: 1.7
  RCP-00001: 1 items total=245.58
  RCP-00002: 3 items total=473.7
  RCP-00003: 1 items total=464.82
  RCP-00004: 1 items total=337.5
  RCP-00005: 1 items total=36.68
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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