将新的MultiIndex设置为pandas DataFrame列会产生ValueError:所有数组的长度必须相同

时间:2014-06-24 11:23:28

标签: python python-2.7 pandas

我有以下DataFrame,名为'pdf':

           1   2   3    4    5    6    7  9
Q45a_1   247  95  62  187  162  216  536  0
Q45a_2   134  48  34   90   79   84  232  0
Q45a_3    59  24  14   40   28   47  112  0
Q45a_4    50  24  10   25   21   30   98  0
Q45a_5    41  27  16   15   17   26   52  0
Q45a_6    31  15   8   15   23   22   50  0
Q45a_7    48  23   9   65   46   52  119  0
Q45a_8   198  66  43  154  131  155  422  0
Q45a_9   227  81  55  164  155  177  489  0
Q45a_10  173  77  49  120  103  162  375  0
Q45a_11   81  42  22   45   45   68  148  0
Q45a_98   18  22   8   11    9   16   64  0
Q45a_99   25  17   4    9    9   13   47  0

我正在尝试使用此MultiIndex重新定义/设置列:

Question         Values
bpcn             1     
                 2     
                 3     
                 4     
                 5     
                 6     
                 7     
                 9  

使用此代码:

pdf.columns = pd.MultiIndex.from_product(['bpcn', pdf.columns], names=['Question','Values'])

虽然此行本身不会产生错误,但此后DataFrame被破坏,并且在引用时返回以下错误:

Traceback (most recent call last):
  File "C:\eclipse\plugins\org.python.pydev_3.5.0.201405201709\pysrc\pydevd_comm.py", line 915, in doIt
    result = pydevd_vars.evaluateExpression(self.thread_id, self.frame_id, self.expression, self.doExec)
  File "C:\eclipse\plugins\org.python.pydev_3.5.0.201405201709\pysrc\pydevd_vars.py", line 482, in evaluateExpression
    sys.stdout.write('%s\n' % (result,))
  File "C:\Anaconda\lib\site-packages\pandas\core\base.py", line 34, in __str__
    return self.__bytes__()
  File "C:\Anaconda\lib\site-packages\pandas\core\base.py", line 46, in __bytes__
    return self.__unicode__().encode(encoding, 'replace')
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 464, in __unicode__
    line_width=width, show_dimensions=show_dimensions)
  File "C:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1299, in to_string
    formatter.to_string()
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 441, in to_string
    strcols = self._to_str_columns()
  File "C:\Anaconda\lib\site-packages\pandas\core\format.py", line 363, in _to_str_columns
    for i, c in enumerate(frame):
  File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 647, in __iter__
    return iter(self._info_axis)
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 666, in __iter__
    return iter(self.values)
  File "C:\Anaconda\lib\site-packages\pandas\core\index.py", line 2486, in values
    self._tuples = lib.fast_zip(values)
  File "lib.pyx", line 487, in pandas.lib.fast_zip (pandas\lib.c:9827)
ValueError: all arrays must be same length

令人困惑的是,我可以使用相同的MultiIndex方法重新定义/设置索引而不会出现问题,例如:

pdf.index = pd.MultiIndex.from_product(['Q45a', pdf.index], names=['Question','Values'])

这给了我:

                    1   2   3    4    5    6    7  9
Question Values                                     
Q45a     Q45a_1   247  95  62  187  162  216  536  0
         Q45a_2   134  48  34   90   79   84  232  0
         Q45a_3    59  24  14   40   28   47  112  0
         Q45a_4    50  24  10   25   21   30   98  0
         Q45a_5    41  27  16   15   17   26   52  0
         Q45a_6    31  15   8   15   23   22   50  0
         Q45a_7    48  23   9   65   46   52  119  0
         Q45a_8   198  66  43  154  131  155  422  0
         Q45a_9   227  81  55  164  155  177  489  0
         Q45a_10  173  77  49  120  103  162  375  0
         Q45a_11   81  42  22   45   45   68  148  0
         Q45a_98   18  22   8   11    9   16   64  0
         Q45a_99   25  17   4    9    9   13   47  0

我必须遗漏一些东西,但为什么同样的方法对DataFrame索引和列都不起作用?

修改

我认为这必定是MultiIndex.from_product()的错误,因为使用MultiIndex.from_tuples()的长版本有效:

pdf.index = pd.MultiIndex.from_product(['Q45a', pdf.index], names=['Question','Values'])
pdf.columns = pd.MultiIndex.from_tuples([('bpcn', 1), ('bpcn', 2), ('bpcn', 3), ('bpcn', 4), ('bpcn', 5), ('bpcn', 6), ('bpcn', 7), ('bpcn', 9)], names=('Question', 'Values'))

给出我想要开头的内容:

Question          bpcn                               
Values               1   2   3    4    5    6    7  9
Question Values                                      
Q45a     Q45a_1    247  95  62  187  162  216  536  0
         Q45a_2    134  48  34   90   79   84  232  0
         Q45a_3     59  24  14   40   28   47  112  0
         Q45a_4     50  24  10   25   21   30   98  0
         Q45a_5     41  27  16   15   17   26   52  0
         Q45a_6     31  15   8   15   23   22   50  0
         Q45a_7     48  23   9   65   46   52  119  0
         Q45a_8    198  66  43  154  131  155  422  0
         Q45a_9    227  81  55  164  155  177  489  0
         Q45a_10   173  77  49  120  103  162  375  0
         Q45a_11    81  42  22   45   45   68  148  0
         Q45a_98    18  22   8   11    9   16   64  0
         Q45a_99    25  17   4    9    9   13   47  0

编辑2

对编辑道歉,但我想我应该确认即使上面的“工作”示例仍然被破坏(如果您尝试访问pdf.T,您将得到与上面相同的错误)。相当肯定from_product()就是问题所在。

另外,我认为我应该为此提供解决方案来避免这个问题。我使用的形式是from_tuples():

pdf.index = pd.MultiIndex.from_tuples([('Q45a', val) for val in pdf.index], names=('Question', 'Values'))
pdf.columns = pd.MultiIndex.from_tuples([('bpcn', val) for val in pdf.columns], names=('Question', 'Values'))

0 个答案:

没有答案
相关问题