Pierpaolo
Pierpaolo

Reputation: 1741

reindex a pandas.panel with a MultiIndex

I was trying to reindex a Panel with a MultiIndex. The ultimate goal is to be able to do slicing like this:

wp.loc[(1,slice(None)),:,:] #Access the panel fixing the first index of the multiIndex

But whenever I try to reindex, like in this example I got this strange behaviour.

wp = pd.Panel(pd.randn(4, 5, 4), items=['Item_1_1', 'Item_1_2','Item_2_1','Item_2_2'],major_axis=['a','b','c','d','e'],minor_axis=['A', 'B', 'C', 'D'])
a=[tuple([int(i) for i in item.split('_')[1:]]) for item in wp.items]
swp=wp.reindex(items=x)

But when I try to access an item of the panel I got;

In [101]: swp[1,1]
Out[101]: 
    A   B   C   D
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c NaN NaN NaN NaN
d NaN NaN NaN NaN
e NaN NaN NaN NaN

What am I doing wrong?

Upvotes: 1

Views: 104

Answers (1)

emveebeeare
emveebeeare

Reputation: 139

Your new index is a list of tuples, so each item is now indexed at (1,1), (1,2), etc. In order to access the item I assume your trying to access with swp[1,1] you have to simply index swp[(1,1)] (i.e. panel[item_name]).

Upvotes: 1

Related Questions