close

How to write Python Array into Excel Spread sheet

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to write Python Array into Excel Spread sheet in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to write Python Array into Excel Spread sheet?

  1. How to write Python Array into Excel Spread sheet?

    Use pandas data frame!
    import pandas as pd array = [['a1', 'a2', 'a3'], ['a4', 'a5', 'a6'], ['a7', 'a8', 'a9'], ['a10', 'a11', 'a12', 'a13', 'a14']]

  2. write Python Array into Excel Spread sheet

    Use pandas data frame!
    import pandas as pd array = [['a1', 'a2', 'a3'], ['a4', 'a5', 'a6'], ['a7', 'a8', 'a9'], ['a10', 'a11', 'a12', 'a13', 'a14']]

Method 1

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:

enter image description here

Method 2

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.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read