简体   繁体   中英

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?

您的新索引是一个元组列表,因此现在每个项目的索引都为(1,1),(1,2)等。为了访问该项目,我假设您尝试使用swp[1,1]进行访问必须简单地索引swp[(1,1)] (即panel[item_name] )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM