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

问题描述 投票:2回答:1

我想在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()
python
1个回答
0
投票

1)如果你想将nu值放在python的列表中,只需在for循环外声明列表:

listOfNu=[]

然后在这段代码中,您可以将值附加到列表中:

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

2)然后导出excel文件中的列表(通过使用xlwt安装依赖项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

© www.soinside.com 2019 - 2024. All rights reserved.