Reputation: 51
I have a code which gives predictions for a case based reasoning model and I store predictions at Result_List . I have N experiments and each experiment I want to store predictions in one excel file at Nth columns. Instead now I can only store them in different excel files named with out + t which is the experiment number being processed. Could you please help be to figure out how I can store all information in one excel file with different columns per experiment. This is the part where I write predictions to excel. t is the experiment number (N) Y_test has a length of integer
##############################WRITE TO EXCEL
import xlsxwriter
wb=xlsxwriter.Workbook("out"+ str(t) + ".xlsx")
sheet1=wb.add_worksheet("out"+ str(t) +"results")
for g in range (len(y_test)):
sheet1.write((g),0,str(Result_List[g]))
wb.close()
################################################
Upvotes: 0
Views: 725
Reputation: 23089
You can use the same basic code you're using now to do this. You just need to iterate over all the data, and specify the row and column numbers correctly in your call to sheet1.write
.
I don't have your data to work with, but let's assume that you ran 6 tests, each of which produced 4 resulting values. And you want each test to be 1 column. Here's how you might have that data stored, and how you could write it to a single sheet:
tests = [
[1, 2, 3, 4],
[11, 22, 33, 44],
[6, 7, 8, 9],
[4, 3, 2, 1],
[45, 33, 77, 223],
[1, -2, -3, -4]
]
wb = xlsxwriter.Workbook("/tmp/out.xlsx")
sheet1 = wb.add_worksheet("results")
for test_num, test in enumerate(tests):
for sample_num, r in enumerate(test):
sheet1.write(sample_num, test_num, r)
wb.close()
That would produce this spreadsheet:
You could add a little more logic if you wanted row and/or column headers.
Upvotes: 1
Reputation: 182
You can use something like sheet1.cell(row=..,column=..)
, where the column and row numbers can be the loop values. This way for each experiment you create a new column and then write the value in the row.
Check out this link for more info: Write the output from "for" loop to excel in PYTHON
Upvotes: 0