← 返回题库
初级

存货估测:账面成本历史趋势分析

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
"""
金融风险管理 - q032
存货估测:存货账面成本历史趋势分析
"""

metadata = {
    "id": "q032",
    "title": "存货估测:账面成本历史趋势分析",
    "module": "存货估测与回归分析",
    "difficulty": "beginner",
    "data_files": ["case33_fish_inventory.csv"],
    "skills": ["趋势分析", "存货变动", "历史数据"],
    "estimated_minutes": 8
}

"""
【题目】
读取case33_fish_inventory.csv,分析渔业公司库存鱼账面成本的历史趋势。

计算:
1. 各年度账面成本同比增长率(保留4位小数)
2. 账面成本最高年份和最低年份

输出DataFrame,包含:year、book_cost、stocking_amount、catch_amount、账面成本增长率
(第一年增长率显示NaN)
"""

def solve():
    import pandas as pd
    from pyodide.http import open_url
    from io import StringIO

    BASE_URL = "https://data.zuihe.com/finance/"
    df = pd.read_csv(StringIO(open_url(BASE_URL + "case33_fish_inventory.csv").read()))
    df = df.sort_values('year').reset_index(drop=True)

    df['账面成本增长率'] = df['book_cost'].pct_change().round(4)
    result = df[['year', 'book_cost', 'stocking_amount', 'catch_amount', '账面成本增长率']]
    return result.to_string()

if __name__ == "__main__":
    print(solve())

示例

输入
solve()
期望输出
year  book_cost  stocking_amount  catch_amount  账面成本增长率 | 0  2012       1777             43.7         46.80      NaN | 1  2013       1755            160.4         77.30  -0.0124 | 2  2014       1411            279.6         62.00  -0.1960 | 3  2015        917            203.5         77.20  -0.3501 | 4  2016        911            149.0         55.20  -0.0065 | 5  2017       1093            121.9         75.40   0.1998 | 6  2018       1154            166.0         65.36   0.0558
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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