Creating an empty MultiIndex

The solution is to leave out the labels. This works fine for me:

>>> import pandas as pd
>>> my_index = pd.MultiIndex(levels=[[],[],[]],
...                          codes=[[],[],[]],
...                          names=[u'one', u'two', u'three'])
>>> my_index
MultiIndex([], names=['one', 'two', 'three'])
>>> my_columns = [u'alpha', u'beta']
>>> df = pd.DataFrame(index=my_index, columns=my_columns)
>>> df
Empty DataFrame
Columns: [alpha, beta]
Index: []
>>> df.loc[('apple','banana','cherry'),:] = [0.1, 0.2]
>>> df
                    alpha beta
one   two    three
apple banana cherry   0.1  0.2

For Pandas Version < 0.25.1:
The keyword labels can be used in place of codes

Leave a Comment