import xlrd
import numpy
fileWorkspace = 'C://Users/jod/Desktop/'
wb1 = xlrd.open_workbook(fileWorkspace + 'assign2.xls')
sh1 = wb1.sheet_by_index(0)
time,amount,category = [],[],[]
for a in range(2,sh1.nrows):
time.append(int(sh1.cell(a,0).value)) # Pulling time from excel (column A)
amount.append(float(sh1.cell(a,1).value)) # Pulling amount from excel (column B)
category.append(str(sh1.cell(a,2).value)) # Pulling category from excel (column C)
#print(time)
#print(amount)
#print(category)
print('\n')
p_p2 = str(sh1.cell(0,1))
p_p1 = p_p2.replace("text:'","")
pp = p_p1.replace("'","")
print(pp) # Printing the type of pay period (Row 1, col B)
c_p2 = str(sh1.cell(1,1))
c_p1 = c_p2.replace("text:'","")
cp = c_p1.replace("'","")
print(cp) # Printing the type of compound period (Row 2, col B)
netflow = 0
outflow = 0
inflow = 0
flow = 0
cat = ["Sales", "Salvage", "Subsidy", "Redeemable", "Utility", "Labor",
"Testing", "Marketing", "Materials", "Logistics"]
if pp == "Years" and cp == "Years": # if pay period and compound period are both in years
IRR = numpy.irr(amount) * 100 # Calculates the internal rate of return (IRR)
print ("IRR:", round(IRR, 2), '%', '\n') # prints (IRR)
for i in time: # for every value in time array
if cat[5] in category: # if "Labor" for cat array is in category array or not
# calculates the present values using all the amount values (col B) instead of
# just using the ones that has "Labor" category label beside them
# Need to make every other value 0, such as beside "Redeemable" and "Salvage"
flow = amount[i]/numpy.power((1 + (IRR/100)), time[i])
if flow>0:
inflow = inflow + flow
if flow<0:
outflow = outflow + flow
print ('Present Value (P) is:', round(flow,0), '\n')
netflow = outflow + inflow
print("In year 0 or current year")
print("-------")
print ('Outflow is: ', round(outflow,0))
print ('Inflow is: ', round(inflow,0))
print ('Netflow is: ', round(netflow,0), '\n')
outflow2 = (round(outflow,0))*(1+(IRR/100))**(9)
inflow2 = (round(inflow,0))*(1+(IRR/100))**(9)
netflow2 = outflow2 + inflow2
print("In year 9")
print("-------")
print ('Outflow is: ', round(outflow2,0))
print ('Inflow is: ', round(inflow2,0))
print ('Netflow is: ', round(netflow2,0), '\n')
我已經評論了重要的代碼行以供說明。 原始問題:使用IRR(Python)計算僅一個類別的未來值
說明了按類別劃分的主要項目收入和費用在第9年的項目未來價值的百分比。圖表還必須清楚地表明該項目在第9年的未來總價值以及內部收益率。
項目可能由10個收入和成本類別組成。這些類別包括:銷售,補救,補貼,可贖回,公用設施,勞動力,測試,營銷,材料和物流。所有的收入和支出將落入這十個類別中的一個。項目支付期和複合期將在Excel工作表頂部標識。薪酬期和複合期可以指定爲以下任何一種:年,季,月。
我越來越困惑,因爲我無法從「勞動」旁邊拉唯一值,「可贖回」,或「打撈」。我只是不知道我犯了什麼錯誤,或者有什麼是不完整的。下面是Excel文件圖像:
Excel File Image 2 Excel File Image 3
41行錯誤:'flow_per_year [INT(sh1.cell_value(R,0))] + = sh1.cell_value(R,1)#unadjusted現金flows''IndexError:列表索引超出range' –
的對不起,我想我認爲那是來自所提供圖像的表單中的所有行。我已將所有sh1.nrows更改爲值'13'。試試看看是否有效? –
它確實給了我結果。但是,它一次打印所有內容,然後再次打印所有內容。另外,我對你的代碼是什麼都很困惑。讓我看看你到底在做什麼,我會盡快回復你。 –