【问题标题】:Write results to Excel将结果写入 Excel
【发布时间】:2016-08-20 11:14:37
【问题描述】:

我对 python 编程很陌生。我正在尝试使用 python xlwt 模块编写结果来表现出色。错误是 TypeError: 'float' object is not iterable. 我无法修复它。任何人都可以帮我解决这个问题。我很感激任何帮助。

from math import *
import pylab as z;
from matplotlib import style
import numpy as np;
from xlwt import *
import xlwt


SMALL_FONT =("Verdana", 8)
style.use('ggplot')

def PriceMovements(S0, down, up, totalsteps, upsteps):
    S = S0*(pow(up, upsteps))*(pow(down, totalsteps-upsteps))
    return S

def binomial(d, u, p):
    g = np.random.binomial(1,p)

    if g == 1:
        return u
    else:
        return d
def write(self, r, c, label =""):
    self.row(r).write(c, label)

#Use the following numbers to console the setting for binomial graph

nodes = 8 #Nodes
S = 100.0 #Initial spot price
u = 1.1346 #Up factor
d = 0.8814 #Down factor
p = 0.7844 # Probability
r = 1.08 #1+Interest rate
n = 3 #Steps

numberofpaths = 2**nodes
valuelist = [] #Emptylist
z.figure(0) #Generating the figure
temp = S #Temporary variable

for i in range(0, numberofpaths, 1):
    valuelist =[]
    S = temp;
    for c in range(0, nodes + 1, 1):
        valuelist.append(S)
        S = S*binomial(d, u, p)
    z.plot(range(0, nodes + 1, 1), valuelist)
    for i in range(nodes):
        z.text(i+.2,valuelist[i]-25,'{:4.1f}'.format(valuelist[i]))

wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')
for r, row in enumerate(valuelist):
    for c, col in enumerate(row):
        ws.write(r, 2+c, lable = col)


wb.save('exampleResult.xls')

【问题讨论】:

    标签: python excel numpy pandas xlwt


    【解决方案1】:

    IIUC 你需要枚举列表valuelist,因为rowtype float 的值:

    print valuelist
    [100.0, 113.46000000000001, 100.00364400000001, 113.46413448240001, 128.73640698373106,
     146.06432736374126, 165.72458582690084, 146.06964994783038, 165.73062483080835]
    
    for r, row in enumerate(valuelist):
        print r
        print row
        ws.write(r, 2+r, label = row)
    
        0
        100.0
        1
        113.46
        2
        100.003644
        3
        113.464134482
        4
        128.736406984
        5
        146.064327364
        6
        165.724585827
        7
        146.069649948
        8
        165.730624831
    
    wb.save('exampleResult.xls')
    

    通过评论编辑:

    我认为您需要在每个循环中创建由valuelist 填充的lists 中的新list。然后就可以创建DataFrame,写成to_excel

    import pandas as pd
    ...
    
    numberofpaths = 2**nodes
    valuelist = [] #Emptylist
    #add listofvaluelists 
    listofvaluelists = []
    z.figure(0) #Generating the figure
    temp = S #Temporary variable
    
    for i in range(0, numberofpaths, 1):
        valuelist =[]
        S = temp;
        for c in range(0, nodes + 1, 1):
            valuelist.append(S)
            S = S*binomial(d, u, p)
        z.plot(range(0, nodes + 1, 1), valuelist)
        #append listofvaluelists
        listofvaluelists.append(valuelist)
        for i in range(nodes):
            z.text(i+.2,valuelist[i]-25,'{:4.1f}'.format(valuelist[i]))
    
    #print listofvaluelists
    #create new DataFrame
    df = pd.DataFrame(listofvaluelists)
    print df
    #write DataFrame to excel
    df.to_excel('exampleResult.xls', sheet_name='Sheet1')
    

    【讨论】:

    • 非常感谢您的帮助。
    • 其实我并没有从python得到所有结果。
    • 每行只传递一个结果。例如,A 行 = 100.00,B 行 = 113.46; C 行 = 100.00,D 行 = 113.46,依此类推。我认为它没有将结果传递到列中(应该是这样)。事实上,当我们打印时,我们获得了我想将其全部传递给 excel 的数据系列。
    • @jezael 非常感谢您的帮助。它有效。
    • 超级。 :) 很高兴能帮助你。
    猜你喜欢
    • 2021-01-19
    • 2016-01-20
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多