我有一些代码以多个列表的形式通过多个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)。提前致谢。 :)
答案 0 :(得分:1)
我无法在计算机上重现相同的输出。但这不是问题。
为什么只有最后一行是因为您正在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]