gpt4 book ai didi

python - 使用 MultiIndex 重新索引 pandas.panel

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:17 24 4
gpt4 key购买 nike

我试图用多重索引重新索引面板。最终目标是能够像这样进行切片:

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

但是每当我尝试重新索引时,就像在这个例子中一样,我都会遇到这种奇怪的行为。

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)

但是当我尝试访问面板中的某个项目时;

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

我做错了什么?

最佳答案

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

关于python - 使用 MultiIndex 重新索引 pandas.panel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904189/

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