"""从 CSV 读取并按地区分组求和。""" from __future__ import annotations from io import StringIO import pandas as pd CSV = """region,product,amount east,a,10 east,b,20 west,a,5 west,b,15 """ def load(text: str = CSV) -> pd.DataFrame: return pd.read_csv(StringIO(text)) def totals(frame: pd.DataFrame) -> pd.DataFrame: grouped = frame.groupby("region", as_index=False)["amount"].sum() return grouped.sort_values("region").reset_index(drop=True) def main() -> None: print(f"rows: {len(load())}") print(f"totals: {totals(load()).to_dict('records')}") if __name__ == "__main__": main()