标题:请求帮忙看下这个问题怎么解决,我按照书上敲的代码
取消只看楼主
yt414204458
Rank: 2
等 级:论坛游民
帖 子:260
专家分:55
注 册:2008-3-1
结帖率:50%
已结贴  问题点数:5 回复次数:0 
请求帮忙看下这个问题怎么解决,我按照书上敲的代码
import pandas as pd
import numpy as np
data=[{'period':'2010','ROE':'8.31%'},{'period':'2011','ROE':'9.77%'},{'period':'2012','ROE':'5.8%'},{'period':'2013','ROE':'5.4%'},{'period':'2014','ROE':'2.08%'},{'period':'2015','ROE':'4.00%'},{'period':'2016','ROE':'4.14%'},{'period':'2017','ROE':'2.26%'},{'period':'2018','ROE':'5.02%'},{'period':'2019','ROE':'5.28%'}]
preROE=0
prePeriod=" "    #用于存储上年度的年份
roe=0
length=len(data)
prePeriod=data[0]["period"]
preROE=data[0]["ROE"]
i=1
while(i<length):
    period=data[i]["period"]
    roe=data[i]["ROE"]
    growth=roe-preROE
    growth='{:.2%}'.format(growth)
    print("{}年的净资产收益率比{}年的净资产收益率增加了{}".format(period,prePeriod,growth))
    prePeriod=period
    preROE=roe
    i=i+1



目前运行,会报错TypeError: unsupported operand type(s) for -: 'str' and 'str'
搜索更多相关主题的帖子: length 代码 data 请求 format 
2022-12-21 11:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510991-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.231683 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved