2
我正在通过documentation关于Pandas中的分层索引。我试图从测试它的范例,具有层次索引创建一个空的数据框中:ValueError:长度不匹配:期望轴在熊猫数据框中创建分层列时有0个元素
In [5]: df = pd.DataFrame()
In [6]: df.columns = pd.MultiIndex(levels = [['first', 'second'], ['a', 'b']], labels = [[0, 0, 1, 1], [0, 1, 0, 1]])
然而,它抛出一个错误:
ValueError Traceback (most recent call last)
<ipython-input-6-dd823f9b8d22> in <module>()
----> 1 df.columns = pd.MultiIndex(levels = [['first', 'second'], ['a', 'b']], labels = [[0, 0, 1, 1], [0, 1, 0, 1]])
/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py in __setattr__(self, name, value)
2755 try:
2756 object.__getattribute__(self, name)
-> 2757 return object.__setattr__(self, name, value)
2758 except AttributeError:
2759 pass
pandas/src/properties.pyx in pandas.lib.AxisProperty.__set__ (pandas/lib.c:44873)()
/usr/local/lib/python3.4/dist-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
446
447 def _set_axis(self, axis, labels):
--> 448 self._data.set_axis(axis, labels)
449 self._clear_item_cache()
450
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in set_axis(self, axis, new_labels)
2800 raise ValueError('Length mismatch: Expected axis has %d elements, '
2801 'new values have %d elements' %
-> 2802 (old_len, new_len))
2803
2804 self.axes[axis] = new_labels
ValueError: Length mismatch: Expected axis has 0 elements, new values have 4 elements
我看不到我的代码任何问题。任何想法发生了什么?
谢谢!另外,我从来没有见过像“pd.np.”这样的东西。你能详细解释一下吗? – Peaceful
不客气。 'pd.np'对于'numpy是一个简短的手段,因为np; np ...',所以你可以从'pandas.np'模块访问numpy函数,而不必显式地导入numpy模块。 – Psidom