ValueError将多索引熊猫数据框转换为Excel

时间:2019-05-11 04:20:05

标签: excel pandas dataframe multi-index

我正在尝试将具有完整行索引和列标签的多索引熊猫数据框导出到Excel。我还希望合并第一列中的“ Pool”索引行,我相信pd.to_excel应该可以这样做。

我也尝试过openpyxl,但是如果没有ValueError,似乎无法使它正常工作。我还尝试了df = df.reset_index()只是为了查看是否可以得到一个显示所有索引和列标签的平面文件,而这没有用。下面是代码和结果:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> import math
>>> s1arrays = [np.array(['Pool1', 'Pool1', 'Pool2', 'Pool2']),
...             np.array(['Rate1', 'Rate2', 'Rate1', 'Rate2'])]
>>> tuples = list(zip(*s1arrays))
>>> index = pd.MultiIndex.from_tuples(tuples, names=['Pool', 'Rate'])
>>> df = pd.DataFrame(np.random.randn(4, 3), columns=[2019, 2020, 2021], index=index)
>>> print(df)
                 2019      2020      2021
Pool  Rate                               
Pool1 Rate1  0.564911 -0.883633 -0.333450
      Rate2 -1.043308  1.543050  1.342350
Pool2 Rate1 -0.838110  2.287242 -1.285863
      Rate2  0.076783 -1.074720  0.801417
>>> df.to_excel('Test Output.xlsx', sheet_name='Sheet1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py", line 2127, in to_excel
    engine=engine)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/formats/excel.py", line 662, in write
    freeze_panes=freeze_panes)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/excel.py", line 1605, in write_cells
    xcell.value, fmt = self._value_with_fmt(cell.val)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 252, in value
    self._bind_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 218, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert 'Pool1' to Excel

在这种情况下,“无法将{0!r}转换为Excel” .format(value))是什么意思?

2 个答案:

答案 0 :(得分:1)

我尝试了您的代码并获得了正确的结果(我无法复制您的错误):

enter image description here

我有 Python 版本3.7.0, Pandas 版本0.23.4和 Jupyter 版本1.0.0。 也许您应该升级安装?

顺便说一句:您可以像这样定义s1arrays

s1arrays = [['Pool1', 'Pool1', 'Pool2', 'Pool2'],
            ['Rate1', 'Rate2', 'Rate1', 'Rate2']]

另一句话是,文件名中的空格是不好的做法。改变 文件名例如到 Test_Output.xlsx

答案 1 :(得分:1)

问题在旧版本的熊猫中,对我来说,在上一个版本中效果很好:

pandas 0.24.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2 

所以请升级您的熊猫版本。