1

I'm trying to export a list of tuples to Excel via Pandas Datarame but every time i try to run the function, i get:

TypeError: init() got an unexpected keyword argument "engine"

The list of tuples is something like

[(83, 97), (34, 78), (39, 70), (60, 66), (90, 48)...]

Here is the code I'm using:

#Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(results, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)

Does anyone know why this is occurring? Is this a case of a missing module? I'm working in an area that has no access to the internet so I can't download/update libraries. Could there be a different alternative to passing a list of tuples easily to an excel file?

JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
who_lee_oh
  • 119
  • 1
  • 2
  • 9
  • Does it have to output as an excel workbook, or would CSV suffice? You could change your list of tuples to a dataframe then output as a csv quite easily... – flyingmeatball Jun 25 '15 at 21:40
  • @flyingmeatball In this case, a CSV sufficed. As my tool progresses, I might want to format the data being based to excel. – who_lee_oh Jun 26 '15 at 15:54

3 Answers3

5

On my CentOS, I got your same exact problem. This was easily addressed with

pip install xlsxwriter

(on your system you might have to do something a bit different; nevertheless, install this package).


Following that, the problem changed to

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

However,

df.to_excel('data.xls', sheet_name='Sheet1', index=False, engine='xlsxwriter')

works.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Thanks @Ami Tavory. This worked for my computer at my cubicle but since the tool is being developed elsewhere where no internet access is available, pip was not able to connect to the url where xlsxwriter resides. – who_lee_oh Jun 26 '15 at 15:56
2

It depends on the version of openpyxl and pandas you have, but maybe this answer to this similar question will help. Just add the line:

import pandas as pd
pd.core.format.header_style = None

when you import pandas, which deletes the formatting dictionary that is hard-coded in for the index row and header column in pandas.

More detailed discussion is in this answer too. Basically, openpyxl version 2 deprecated the style-conversion functions that map dictionary style definitions to the new openpyxl api, but Pandas still uses the old style, and for some reason the deprecation pass-through function errors out.

Community
  • 1
  • 1
slushy
  • 3,277
  • 1
  • 18
  • 24
1

This works for me using Pandas 14.1.

df = pd.DataFrame([(1, 2), (3, 4)])

>>> df
   0  1
0  1  2
1  3  4
file_path = '~/Downloads/test.xlsx'
df.to_excel(file_path, index=False)

I don't believe there is a need to explicitly set the engine:

excel_writer : string or ExcelWriter object (File path or existing ExcelWriter)

Alexander
  • 105,104
  • 32
  • 201
  • 196
  • There's a reason for the ability to set & control the writer. See [here](http://pbpython.com/improve-pandas-excel-output.html), for example. I would not give this up so fast. – Ami Tavory Jun 25 '15 at 21:51