Search code examples
excelpython-3.xpandaswriter

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


I have some code which gives output through several for loops in the form of multiple lists and I want to write the output in excel or csv file using pandas excel writer.

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()       

This is how print output look like:

[-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]

And this is what I got in 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

I just got the last list out of several lists from the print output copied in the excel file but what I want is the complete output (Xbar in above code) to be copied in the excel file. Thanks in advance. :)


Solution

  • I haven't been able to reproduce the same output on my computer. But here isn't the question.

    The reason why you only have the last row is that you're writing the csv file in the for loop. So you only get the last row because you overwrite all of them each time.

    On solution (I think the simplest here), is to save your results over each iteration in a results dataframe (here output_df) and then export this dataframe in a csv file. In the code below, I save the results of each mega loop as a new column of output_df.

    Here the code:

    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()
    

    Output printed:

    #     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]
    

    The csv file: enter image description here