pandas 的 to_excel 函数产生了意外的 TypeError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/59168758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:25:09  来源:igfitidea点击:

The function to_excel of pandas generate an unexpected TypeError

pythonpandasexport-to-excel

提问by mommomonthewind

I created a dictionary of pandas dataframe:

我创建了一个Pandas数据框字典:

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

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

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

所以我假设这d[k]是一个正确的Pandas数据框。

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

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.

我正在使用 Python 3.7,pandas 0.25.3。

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

更新:如果我替换to_excelto_csv,代码运行完美。

回答by Animeta

I have same problem with openpyxl=3.0.2,

我有同样的问题openpyxl=3.0.2

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

请参阅此答案,我将 openpyxl 回滚到 3.0.1(conda 或 pip)并且它可以工作。

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

or

或者

>>> pip uninstall openpyxl
>>> pip install openpyxl==3.0.1