创建列表并将其保存到Excel文件

时间:2019-03-06 11:18:56

标签: python

我想在nu中放入一些值(循环中的list变量值),然后将列表中的export放入excel。有人对我该如何做有一些提示吗?

The values I want in a list

import numpy as np 
import math
from scipy.stats import skew, kurtosis, kurtosistest
import matplotlib.pyplot as plt
from scipy.stats import norm,t
import pandas as pd

for i in range(0, 3000):

    data = pd.read_excel(r"x.xlsx",skipfooter=i,skiprows=3867-i,usecols="C")
    ret = np.array(data.values)



    from scipy.stats import skew, kurtosis
    X = np.random.randn(10000000)
    print(skew(X))
    print(kurtosis(X, fisher=False))

    # N(x; mu, sig) best fit (finding: mu, stdev)
    mu_norm, sig_norm = norm.fit(ret)
    dx = 0.0001  # resolution
    x = np.arange(-0.1, 0.1, dx)
    pdf = norm.pdf(x, mu_norm, sig_norm)
    print("Integral norm.pdf(x; mu_norm, sig_norm) dx = %.2f" % (np.sum(pdf*dx)))
    print("Sample mean  = %.5f" % mu_norm)
    print("Sample stdev = %.5f" % sig_norm)
    print()

    df = pd.DataFrame(ret)

    # Student t best fit (finding: nu)
    Parm = t.fit(ret)
    nu, mu_t, sig_t = Parm
    pdf2 = t.pdf(x, nu, mu_t, sig_t)
    print("Integral t.pdf(x; mu, sig) dx = %.2f" % (np.sum(pdf2*dx)))
    print("nu = %.2f" % nu)
    print()

1 个答案:

答案 0 :(得分:0)

1) If you want to put nu values in a list in python just declare the list outside the for loop:

listOfNu=[]

Then in this piece of code you can append to your list the values:

....
print("nu = %.2f" % nu)
listOfNu.append(nu)
print()
...

2) Then to export the list in an excel file (use dependency xlwt by installing it with sudo easy_install xlwt):

import xlwt
from tempfile import TemporaryFile
book = xlwt.Workbook()
sheet1 = book.add_sheet('nu_example_sheet')

for i,e in enumerate(listOfNu):
    sheet1.write(i,1,e)

name = "nu_example_file.xls"
book.save(name)
book.save(TemporaryFile())

Example of the output file with a list of random numbers