Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The function to_excel of pandas generate an unexpected TypeError

I created a dictionary of pandas dataframe:

d[k] = pd.DataFrame(data=data[i])

So I assume that d[k] is a correct pandas dataframe.

Then

for k in d.keys():
  d[k].to_excel (file_name)

Then I have the error:

TypeError: got invalid input value of type <class 'xml.etree.ElementTree.Element'>, expected string or Element

I am using Python 3.7, pandas 0.25.3.

Update: if I replace to_excel by to_csv, the code runs perfectly.

like image 709
mommomonthewind Avatar asked Dec 04 '19 04:12

mommomonthewind


People also ask

What is the difference between xlsxwriter and to_excel in pandas?

Side issue: in constant_memory mode XlsxWriter requires that data in written in row x column order but the Pandas Excel writer works in column x row order. So I wonder if the constant_memory option is actually getting passed or if the dataframe is actually being written correctly. .to_excel only takes columns that are type: object.

How do I read an Excel file into a pandas Dataframe?

Read an Excel file into a pandas DataFrame. Read a comma-separated values (csv) file into DataFrame. For compatibility with to_csv () , to_excel serializes lists and dicts to strings before writing. Once a workbook has been saved it is not possible to write further data without rewriting the whole workbook. To specify the sheet name:

What happened to xlwt engine in pandas?

Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. Write MultiIndex and Hierarchical Rows as merged cells.


Video Answer


1 Answers

I have same problem with openpyxl=3.0.2,

Refer to this answer, I roll openpyxl back to 3.0.1(conda or pip) and it works.

>>> conda remove openpyxl
>>> conda install openpyxl==3.0.1

or

>>> pip uninstall openpyxl
>>> pip install openpyxl==3.0.1
like image 97
Animeta Avatar answered Oct 18 '22 03:10

Animeta