Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-数据帧导出';Excel的标题和索引导致类型错误_Python_Pandas_Openpyxl - Fatal编程技术网

Python 熊猫-数据帧导出';Excel的标题和索引导致类型错误

Python 熊猫-数据帧导出';Excel的标题和索引导致类型错误,python,pandas,openpyxl,Python,Pandas,Openpyxl,我正在使用pandas 0.15和openpyxl2.2.0作为编写器引擎,但是pandas 0.16也会出现同样的问题 我需要将熊猫数据帧导出到.xlsx文件。数据帧非常简单-类似于: col_A col_B col_C 0 1 100 AB 1 2 200 AC 2 3 300 AD 我可以导出数据框的内容(即没有索引标签和列名): 但是,如果包含索引标签或列名(或两者都包含),则会出现TypeError。这: df

我正在使用
pandas 0.15
openpyxl2.2.0
作为编写器引擎,但是
pandas 0.16
也会出现同样的问题

我需要将熊猫数据帧导出到
.xlsx
文件。数据帧非常简单-类似于:

   col_A  col_B col_C
0      1    100    AB
1      2    200    AC
2      3    300    AD
我可以导出数据框的内容(即没有索引标签和列名):

但是,如果包含索引标签或列名(或两者都包含),则会出现TypeError。这:

df.to_excel(path_to_xlsx_file, sheet_name, header=False, index=True)
或者这个:

 df.to_excel(path_to_xlsx_file, sheet_name, header=True, index=False)
原因如下:

    Traceback (most recent call last):

  File "<ipython-input-11-a60810c2c9c2>", line 1, in <module>
    runfile('F:/mine/python/pandas_tests.py', wdir='F:/mine/python')

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "F:/mine/python/pandas_tests.py", line 25, in <module>
    df.to_excel(path_to_xlsx_file, sheet_name, header=True, index=False)

  File "C:\Python27\lib\site-packages\pandas\util\decorators.py", line 88, in wrapper
    return func(*args, **kwargs)

  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1260, in to_excel
    startrow=startrow, startcol=startcol)

  File "C:\Python27\lib\site-packages\pandas\io\excel.py", line 705, in write_cells
    xcell.style = xcell.style.copy(**style_kwargs)

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\styleable.py", line 107, in style
    protection=self.protection

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\__init__.py", line 42, in __init__
    self._font = font

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\descriptors\base.py", line 35, in __set__
    raise TypeError('expected ' + str(self.expected_type))

TypeError: expected <class 'openpyxl.styles.fonts.Font'>
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('F:/mine/python/pandas\u tests.py',wdir='F:/mine/python')
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第601行,在runfile中
execfile(文件名、命名空间)
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第66行,在execfile中
exec(编译(脚本文本,文件名,'exec'),glob,loc)
文件“F:/mine/python/pandas_tests.py”,第25行,在
df.to_excel(路径到_xlsx_文件,工作表名称,标题=True,索引=False)
文件“C:\Python27\lib\site packages\pandas\util\decorators.py”,第88行,在包装器中
返回函数(*args,**kwargs)
文件“C:\Python27\lib\site packages\pandas\core\frame.py”,第1260行,在excel中
startrow=startrow,startcol=startcol)
文件“C:\Python27\lib\site packages\pandas\io\excel.py”,第705行,在write\u单元格中
xcell.style=xcell.style.copy(**style\u-kwargs)
文件“C:\Python27\lib\site packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\styleable.py”,第107行,在style中
保护=自我保护
文件“C:\Python27\lib\site packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\\uuuuuu init\uuuuu.py”,第42行,在\uuuu init中__
self.\u font=font
文件“C:\Python27\lib\site packages\openpyxl-2.2.0-py2.7.egg\openpyxl\descriptors\base.py”,第35行,在__
raise TypeError('预期'+str(自我预期类型))
TypeError:应为

我不能使用xlsxwriter,因为数据帧必须导出到现有文件。

解决方案是使用openpyxl版本2.1.3或2.1.5

无法复制。对我来说很合适。Python2.7.x和Pandas 0.16您使用的是哪个Python包?我正在Windows7 64位机器上运行python(x,y)2.7.9.0。问题是否与我使用的操作系统或python包有关?返回到openpyxl的早期版本(2.1.3)解决了问题2.1.5是2.1系列中最新的一个。可以通过一个bug报告来帮助追踪这一点,但我怀疑这是由于样式的新处理,尽管严格的键入已经存在了一段时间。我也遇到了这个问题,多亏了snowlion,我确实安装了openpyxl==2.1.3以返回到以前的版本,所以我现在可以导出到excel。
    Traceback (most recent call last):

  File "<ipython-input-11-a60810c2c9c2>", line 1, in <module>
    runfile('F:/mine/python/pandas_tests.py', wdir='F:/mine/python')

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "F:/mine/python/pandas_tests.py", line 25, in <module>
    df.to_excel(path_to_xlsx_file, sheet_name, header=True, index=False)

  File "C:\Python27\lib\site-packages\pandas\util\decorators.py", line 88, in wrapper
    return func(*args, **kwargs)

  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1260, in to_excel
    startrow=startrow, startcol=startcol)

  File "C:\Python27\lib\site-packages\pandas\io\excel.py", line 705, in write_cells
    xcell.style = xcell.style.copy(**style_kwargs)

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\styleable.py", line 107, in style
    protection=self.protection

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\styles\__init__.py", line 42, in __init__
    self._font = font

  File "C:\Python27\lib\site-packages\openpyxl-2.2.0-py2.7.egg\openpyxl\descriptors\base.py", line 35, in __set__
    raise TypeError('expected ' + str(self.expected_type))

TypeError: expected <class 'openpyxl.styles.fonts.Font'>