4

I have a code that generates an numpy.array in each loop. I want to save the array as a row in an excel file (i.e., the array created in the first loop becomes the first row, the array generated in the 2nd loop becomes the 2nd row and so on). The array is created in the following way:

for loop in range(0,10):

Array1 = ..... 

Array2 = ......

Array3 = numpy.concatenate((Array1,Array2),axis=0)

Any idea how I can put Array3 in 10 different rows of an excel file? (If the array is 5 dimensional, the excel file should contain 10 rows and 5 columns).

2 Answers 2

6

Assuming that data is a list of your arrays, and that you made sure that all your arrays have same size, you can use np.savetxt to save your data to a TSV/CSV file, as suggested by @Qnan

np.savetxt(your_output_file, np.array(data), delimiter="\t")

Check the np.savetxt documentation for more information on how to format the fields, if needed.

The idea is to write the file at once, instead of line by line.

Sign up to request clarification or add additional context in comments.

Comments

-1

Export to a TSV or CSV (tab/comma separated value) file, Excel can open those.

Basically if you print the values to a simple text file line by line and separate those in the same line by a tab character ('\t'), Excel will be able to read that file. You might have to call it a .xls file.

2 Comments

You should not call it .xls...that is generally reserved for Excel's binary spreadsheet format. Just call it .txt (for TSV) or .csv (for CSV) and open it with Excel.
@nneonneo that's an option too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.