如何使用pandas excel writer 将数据写入excel 文件?

How to write the data in excel file using pandas excel writer?

我有一些代码以多个列表的形式通过多个 for 循环提供输出,我想使用 pandas excel writer 将输出写入 excel 或 csv 文件.

from pulp import *
from openpyxl import load_workbook
import pandas as pd
import numbers
from pulp import solvers
import xlwt

P=[4.645885257, 4.481959238,    4.160581972,    2.893299763,    2.746552049,    2.762327167,    2.785312466,    2.782704044,\
2.761575576,    2.790301008,    2.826271593,    2.98196142, 3.106517237,    3.049694785,    2.841111886,    2.469119048,\
2.424998603,    2.482937879,    2.541880038,    2.544940077,    2.526766508,    2.539441678,    2.60810043, 2.782490319]
X=[-50, -40, -30, -20, -10, 0, 10, 20, 30, 40]
S=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]

x=10
s=16
n=24

F=[[0 for j in range(x)] for i in range(s)]


def xyz():
    Fbar=list()
    Xbar=list() 
    Mega=[22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
    for k in Mega:
        for f in F:
            try:
                FFF=max([x for x in f if isinstance(x, numbers.Number)])
                XXX=X[f.index(max([x for x in f if isinstance(x, numbers.Number)]))]
                Fbar.append(FFF)
                Xbar.append(XXX)
            except ValueError:
                FFF="NA"
                Fbar.append(FFF)
                Xbar.append(FFF)
        for i in range(s):
            for j in range(x):
                if 150>=(S[i]+X[j])>=S[Xbar.index(max([x for x in Xbar if isinstance(x, numbers.Number)]))]:
                    FFFFF=(S[i]+X[j])/10
                    F[i][j]=-X[j]*P[k]+Fbar[int(FFFFF)]
                if 150<(S[i]+X[j])<S[Xbar.index(max([x for x in Xbar if isinstance(x, numbers.Number)]))]:
                    F[i][j]="NA"


        Xbar=list()
        for f in F:
            try:
                FFF=max([x for x in f if isinstance(x, numbers.Number)])
                XXX=X[f.index(max([x for x in f if isinstance(x, numbers.Number)]))]
                Fbar.append(FFF)
                Xbar.append(XXX)
            except ValueError:
                FFF="NA"
                Fbar.append(FFF)
                Xbar.append(FFF)
        print(Xbar)

        df= pd.DataFrame(Xbar)
        writer= pd.ExcelWriter('C:\Fourth Term @ Dal\Project\Directive studies\output.xlsx', engine='xlsxwriter')
        df.to_excel(writer, sheet_name='Sheet1', startcol=0, startrow=1, header=False, index=True)
        workbook= writer.book
        writer.save() 

xyz()       

这是打印输出的样子:

[-50, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[40, 40, 30, 20, 10, -50, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50]
[40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0, -10, -20, -30, -40, -50]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[40, 40, 30, 20, 10, 0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0, -10, -20, -30, -40]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0, -10, -20, -30, -40, -50]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0, -10, -20, -30, -40]
[40, 40, 40, 40, 30, 20, 10, 0, -10, -20, -30, -40, -50, -50, -50, -50]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 30, 20, 10, 0]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]
[0, -10, -20, -30, -40, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, -50]

这就是我在 excel (output.xlsx) 中得到的:

0   0
1   -10
2   -20
3   -30
4   -40
5   -50
6   -50
7   -50
8   -50
9   -50
10  -50
11  -50
12  -50
13  -50
14  -50
15  -50

我刚刚从 excel 文件中复制的打印输出中得到了几个列表中的最后一个列表,但我想要的是要在 [=25] 中复制的完整输出(上面代码中的 Xbar) =] 文件。提前致谢。 :)

我无法在我的计算机上重现相同的输出。但这不是问题。

只有最后一行的原因是您在 for 循环中编写 csv 文件。所以你只会得到最后一行,因为你每次都覆盖了所有这些。

解决方案(我认为这里最简单)是将每次迭代的结果保存在结果数据框中(此处 output_df),然后将此数据框导出到 csv 文件中。 在下面的代码中,我将每个 mega 循环的结果保存为 output_df 的新列。

这里是代码:

from pulp import *
from openpyxl import load_workbook
import pandas as pd
import numbers
from pulp import solvers
import xlwt

P = [4.645885257, 4.481959238,    4.160581972,    2.893299763,    2.746552049,    2.762327167,    2.785312466,    2.782704044,
     2.761575576,    2.790301008,    2.826271593,    2.98196142, 3.106517237,    3.049694785,    2.841111886,    2.469119048,
     2.424998603,    2.482937879,    2.541880038,    2.544940077,    2.526766508,    2.539441678,    2.60810043, 2.782490319]
X = [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40]
S = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]

x = 10
s = 16
n = 24

F = [[0 for j in range(x)] for i in range(s)]


def xyz():
    Fbar = list()
    Xbar = list()
    Mega = [22, 21, 20, 19, 18, 17, 16, 15, 14,
            13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

    df_output = pd.DataFrame()

    for count, k in enumerate(Mega):
        for f in F:
            try:
                FFF = max([x for x in f if isinstance(x, numbers.Number)])
                XXX = X[f.index(
                    max([x for x in f if isinstance(x, numbers.Number)]))]
                Fbar.append(FFF)
                Xbar.append(XXX)
            except ValueError:
                FFF = "NA"
                Fbar.append(FFF)
                Xbar.append(FFF)
        for i in range(s):
            for j in range(x):
                if 150 >= (S[i]+X[j]) >= S[Xbar.index(max([x for x in Xbar if isinstance(x, numbers.Number)]))]:
                    FFFFF = (S[i]+X[j])/10
                    F[i][j] = -X[j]*P[k]+Fbar[int(FFFFF)]
                if 150 < (S[i]+X[j]) < S[Xbar.index(max([x for x in Xbar if isinstance(x, numbers.Number)]))]:
                    F[i][j] = "NA"

        Xbar = list()
        for f in F:
            try:
                FFF = max([x for x in f if isinstance(x, numbers.Number)])
                XXX = X[f.index(
                    max([x for x in f if isinstance(x, numbers.Number)]))]
                Fbar.append(FFF)
                Xbar.append(XXX)
            except ValueError:
                FFF = "NA"
                Fbar.append(FFF)
                Xbar.append(FFF)
        print(Xbar)

        df_output["Mega_{0}".format(k)] = Xbar

    print(df_output)
    writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')
    df_output.to_excel(writer, sheet_name='Sheet1', startcol=0, header=True, index=True)
    writer.save()

xyz()

输出打印:

#     Mega_22  Mega_21  Mega_20  Mega_19  Mega_18  Mega_17  Mega_16  ...  Mega_7  Mega_6  Mega_5  Mega_4  Mega_3  Mega_2  Mega_1
# 0       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 1       -10      -10      -10      -10      -10      -10      -10  ...     -10     -10     -10     -10     -10     -10     -10
# 2       -20      -20      -20      -20      -20      -20      -20  ...     -20     -20     -20     -20     -20     -20     -20
# 3       -30      -30      -30      -30      -30      -30      -30  ...     -30     -30     -30     -30     -30     -20     -20
# 4       -40      -40      -40      -40      -40      -40      -40  ...     -40     -40     -40     -40     -40     -30     -30
# 5       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -40     -40
# 6       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 7       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 8       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 9       -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 10      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 11      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 12      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 13      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 14      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50
# 15      -50      -50      -50      -50      -50      -50      -50  ...     -50     -50     -50     -50     -50     -50     -50

# [16 rows x 22 columns]

csv 文件: