I am trying to write following array into Excel spreadsheet using Python:
array = [ [a1,a2,a3], [a4,a5,a6], [a7,a8,a9], [a10, a11, a12, a13, a14]]
At spreadsheet array should look:
a1 a4 a7 a10
a2 a5 a8 a11
a3 a6 a9 a12
a13
a14
Is anyone can show some Python code to do it? Thank you in advance,
Felix
Use “savetxt” method of numpy to save numpy array to csv file. CSV files are easy to share and view, therefore it's useful to convert numpy array to csv. CSV stands for comma separated values and these can be viewed in excel or any text editor whereas to view a numpy array object we need python.
Here is one way to do it using the XlsxWriter module:
import xlsxwriter
workbook = xlsxwriter.Workbook('arrays.xlsx')
worksheet = workbook.add_worksheet()
array = [['a1', 'a2', 'a3'],
['a4', 'a5', 'a6'],
['a7', 'a8', 'a9'],
['a10', 'a11', 'a12', 'a13', 'a14']]
row = 0
for col, data in enumerate(array):
worksheet.write_column(row, col, data)
workbook.close()
Output:
Use pandas data frame!
import pandas as pd
array = [['a1', 'a2', 'a3'],
['a4', 'a5', 'a6'],
['a7', 'a8', 'a9'],
['a10', 'a11', 'a12', 'a13', 'a14']]
df = pd.DataFrame(array).T
df.to_excel(excel_writer = "C:/Users/Jing Li/Desktop/test.xlsx")
excel_writer is File path in str or existing ExcelWriter object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With