gpt4 book ai didi

python - 值错误 : Length mismatch: Expected axis has 0 elements while creating hierarchical columns in pandas dataframe

转载 作者:太空狗 更新时间:2023-10-30 00:38:27 42 4
gpt4 key购买 nike

我正在浏览 documentation关于 Pandas 中的层次索引。我尝试测试其中的示例以创建具有分层索引的空数据框:

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

我没有发现我的代码有任何问题。知道发生了什么吗?

最佳答案

问题是您有一个包含零列的空数据框,并且您正试图为其分配一个四列多索引;如果您最初创建一个四列的空数据框,错误将消失:

df = pd.DataFrame(pd.np.empty((0, 4)))    
df.columns = pd.MultiIndex(levels = [['first', 'second'], ['a', 'b']], labels = [[0, 0, 1, 1], [0, 1, 0, 1]])

或者您可以使用多索引创建空数据框,如下所示:

multi_index = pd.MultiIndex(levels = [['first', 'second'], ['a', 'b']], labels = [[0, 0, 1, 1], [0, 1, 0, 1]])    
df = pd.DataFrame(columns=multi_index)

df
# first second
# a b a b

关于python - 值错误 : Length mismatch: Expected axis has 0 elements while creating hierarchical columns in pandas dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43190773/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com