相关文章推荐
憨厚的烈酒  ·  apache (web服务器) ...·  7 月前    · 
暗恋学妹的酱肘子  ·  DataType 属性 | ...·  1 年前    · 
纯真的杯子  ·  PYQT: ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ValueError: Length mismatch: Expected axis has 0 elements while creating hierarchical columns in pandas dataframe

Ask Question

I was going through the documentation about the hierarchical indexing in Pandas. I tried testing the examples from it to create an empty dataframe with hierarchical indexing:

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]])

However, it throws an error:

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)
    447     def _set_axis(self, axis, labels):
--> 448         self._data.set_axis(axis, labels)
    449         self._clear_item_cache()
/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))
   2804         self.axes[axis] = new_labels
ValueError: Length mismatch: Expected axis has 0 elements, new values have 4 elements

I don't see any problem with my code. Any ideas what is happening?

The problem is that you have an empty data frame which has zero columns, and you are trying to assign a four columns multi-index to it; If you create an empty data frame of four columns initially, the error will be gone:

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]])

Or you can create empty data frame with the multi-index as follows:

multi_index = pd.MultiIndex(levels = [['first', 'second'], ['a', 'b']], labels = [[0, 0, 1, 1], [0, 1, 0, 1]])    
df = pd.DataFrame(columns=multi_index)
#   first    second
#  a    b   a     b
                Thanks! As an aside, I have never seen anything like pd.np.. Can you elaborate a bit on this?
– Peaceful
                Apr 3, 2017 at 17:42
                You're welcome. pd.np is a short hand for import numpy as np; np..., so you can access numpy functions from the pandas.np module without having to import numpy module explicitly.
– Psidom
                Apr 3, 2017 at 17:45
                Btw, using pd.np will be deprecated soon; so don't use it. Just import numpy directly in any new code you write.
– Noah May
                Feb 7, 2021 at 16:25

This solution does not require numpy:

# create empty DataFrame with 4 columns
df = pd.DataFrame(columns = range(4))
df.columns = pd.MultiIndex(
    levels = [['first', 'second'], ['a', 'b']], 
    codes = [[0, 0, 1, 1], [0, 1, 0, 1]]

(Note: I changed labels to codes because that was changed in Pandas v1.0.0)

This error can also occur if you have used df.loc[, <col_name>]= value and you have not wrapped the condition within double brackets (). Make sure to always insert conditions in loc statements in double brackets.

It should be something similar to the one below:

df.loc[<(condition1) & (condition2)>, <col_name>]= value

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Feb 2, 2022 at 6:42

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.