who_lee_oh
who_lee_oh

Reputation: 119

Pandas DataFrame to Excel Problems

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?

Upvotes: 1

Views: 25842

Answers (3)

slushy
slushy

Reputation: 3377

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.

Upvotes: 2

Alexander
Alexander

Reputation: 109726

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)

Upvotes: 1

Ami Tavory
Ami Tavory

Reputation: 76406

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.

Upvotes: 5

Related Questions