excel - 如何使用 pandas excel writer 在 excel 文件中写入数据?

标签 excel python-3.x pandas 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 文件中的打印输出中得到了几个列表中的最后一个列表,但我想要的是要复制到 excel 文件中的完整输出(上面代码中的 Xbar)。提前致谢。 :)

最佳答案

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

您只有最后一行的原因是您正在编写 csv for 中的文件环形。所以你只得到最后一行,因为你每次都覆盖它们。

关于解决方案(我认为这里最简单)是将每次迭代的结果保存在结果数据框中(此处为 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文件:
enter image description here

关于excel - 如何使用 pandas excel writer 在 excel 文件中写入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681889/

相关文章:

python - 如何检查在 Python 中使用 Walrus 运算符时是否按下了 Enter 键?

python Pandas : Find a value in another dataframe and replace it

python - Python光标提取器生成器

python-3.x - SQLite数据存储

excel - (用户窗体)ListBox 列表属性由代码更新时未触发 ListBox 更改事件

vba - 如何使用vba禁用单元格中的更改?

python - Pandas:检查列值是否唯一

python - 如何拆分数据框中的列并将其替换为新列

VBA - 忽略隐藏的工作表 - 将特定工作表另存为 PDF

vba - SQL Excel VBA 运行时错误 3709 无效连接