Nev Pires
Nev Pires

Reputation: 175

Write a numpy.ndarray to an xlsx spreadsheet

I am trying to use xlsxwriter to write a numpy.ndarray to an excel spreadsheet.

The 'type' of my array is numpy.ndarray. The array itself when printed takes this form:

exampleArray = array([[[0.    ,  1], [0.    , 1], [0.    ,1]]])

The code that I am using is as follows:

workbook = xlsxwriter.Workbook('WorkBookName.xlsx')
worksheet = workbook.add_worksheet('workSheetName')

row = 0
col = 0

for data in exampleArray[0]:
    worksheet.write_colum(row, col data)
    col += 1

workbook.close()

I get the error:

AttributeError: 'list' object has no attribute 'tell'

Thanks in advance!

Upvotes: 3

Views: 6159

Answers (1)

jpp
jpp

Reputation: 164773

Using Pandas, this is straightforward. You just need to make sure the array is 2-dimensional.

import pandas as pd

A = np.array([[[0.    ,  1], [0.    , 1], [0.    ,1]]])

# construct dataframe, index [0] to make 2d
df = pd.DataFrame(A[0])

# save to Excel, exclude index and headers
df.to_excel('file.xlsx', index=False, header=False)

Upvotes: 5

Related Questions