温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用pandas怎么层次化索引

发布时间:2021-05-21 16:48:40 来源:亿速云 阅读:195 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用pandas怎么层次化索引,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创建一个Series,并用一个由列表或数组组成的列表作为索引。

data=Series(np.random.randn(10), index=[['a','a','a','b','b','b','c','c','d','d'], [1,2,3,1,2,3,1,2,2,3]]) data Out[6]:  a 1  -2.842857   2  0.376199   3  -0.512978 b 1  0.225243   2  -1.242407   3  -0.663188 c 1  -0.149269   2  -1.079174 d 2  -0.952380   3  -1.113689 dtype: float64

这就是带MultiIndex索引的Series的格式化输出形式。索引之间的“间隔”表示“直接使用上面的标签”。

data.index Out[7]:  MultiIndex(levels=[['a', 'b', 'c', 'd'], [1, 2, 3]], labels=[[0, 0, 0, 1, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 1, 2]])

对于一个层次化索引的对象,选取数据子集的操作很简单:

data['b'] Out[8]:  1  0.225243 2  -1.242407 3  -0.663188 dtype: float64 data['b':'c'] Out[10]:  b 1  0.225243   2  -1.242407   3  -0.663188 c 1  -0.149269   2  -1.079174 dtype: float64 data.ix[['b','d']] __main__:1: DeprecationWarning:  .ix is deprecated. Please use .loc for label based indexing or .iloc for positional indexing See the documentation here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated Out[11]:  b 1  0.225243   2  -1.242407   3  -0.663188 d 2  -0.952380   3  -1.113689 dtype: float64

甚至可以在“内层”中进行选取:

data[:,2] Out[12]:  a  0.376199 b  -1.242407 c  -1.079174 d  -0.952380 dtype: float64

层次化索引在数据重塑和基于分组的操作中扮演重要角色。

可以通过unstack方法被重新安排到一个DataFrame中:

data.unstack() Out[13]:       1     2     3 a -2.842857 0.376199 -0.512978 b 0.225243 -1.242407 -0.663188 c -0.149269 -1.079174    NaN d    NaN -0.952380 -1.113689 #unstack的逆运算是stack data.unstack().stack() Out[14]:  a 1  -2.842857   2  0.376199   3  -0.512978 b 1  0.225243   2  -1.242407   3  -0.663188 c 1  -0.149269   2  -1.079174 d 2  -0.952380   3  -1.113689 dtype: float64

对于DataFrame,每条轴都可以有分层索引:

frame=DataFrame(np.arange(12).reshape((4,3)), index=[['a','a','b','b'],[1,2,1,2]], columns=[['Ohio','Ohio','Colorado'], ['Green','Red','Green']]) frame Out[16]:     Ohio   Colorado   Green Red  Green a 1   0  1    2  2   3  4    5 b 1   6  7    8  2   9 10    11

各层都可以有名字。如果指定了名称,它们会显示在控制台中(不要将索引名称和轴标签混为一谈!)

frame.index.names=['key1','key2'] frame.columns.names=['state','color'] frame Out[22]:  state   Ohio   Colorado color   Green Red  Green key1 key2           a  1    0  1    2    2    3  4    5 b  1    6  7    8    2    9 10    11

由于有了分部的列索引,可以轻松选取列分组:

frame['Ohio'] Out[23]:  color   Green Red key1 key2       a  1     0  1    2     3  4 b  1     6  7    2     9  10

重排分级排序

有时需要重新调整某条轴上各级别的顺序,或根据指定级别上的值对数据进行排序。swaplevel接受两个级别编号或名称,并返回一个互换了级别的新对象(但数据不会发生变化):

frame.swaplevel('key1','key2') Out[24]:  state   Ohio   Colorado color   Green Red  Green key2 key1           1  a    0  1    2 2  a    3  4    5 1  b    6  7    8 2  b    9 10    11

sortlevel则根据单个级别中的值对数据进行排序。交换级别时,常用得到sortlevel,这样最终结果也是有序的了:

frame.swaplevel(0,1) Out[27]:  state   Ohio   Colorado color   Green Red  Green key2 key1           1  a    0  1    2 2  a    3  4    5 1  b    6  7    8 2  b    9 10    11 #交换级别0,1(也就是key1,key2) #然后对axis=0进行排序 frame.swaplevel(0,1).sortlevel(0) __main__:1: FutureWarning: sortlevel is deprecated, use sort_index(level= ...) Out[28]:  state   Ohio   Colorado color   Green Red  Green key2 key1           1  a    0  1    2    b    6  7    8 2  a    3  4    5    b    9 10    11

根据级别汇总统计

有时需要重新调整某条轴上各级别的顺序,或根据指定级别上的值对数据进行排序。swaplevel接受两个级别编号或名称,并返回一个互换了级别的新对象(但数据不会发生变化):

frame.sum(level='key2') Out[29]:  state Ohio   Colorado color Green Red  Green key2           1     6  8    10 2    12 14    16 frame.sum(level='color',axis=1) Out[30]:  color   Green Red key1 key2       a  1     2  1    2     8  4 b  1    14  7    2    20  10

使用DataFrame的列

将DataFrame的一个或多个列当做行索引来用,或将行索引变成Dataframe 的列。

frame=DataFrame({'a':range(7),'b':range(7,0,-1), 'c':['one','one','one','two','two','two','two'], 'd':[0,1,2,0,1,2,3]}) frame Out[32]:    a b  c d 0 0 7 one 0 1 1 6 one 1 2 2 5 one 2 3 3 4 two 0 4 4 3 two 1 5 5 2 two 2 6 6 1 two 3

DataFrame的set_index函数会将其一个或多个列转换为行索引,并创建一个新的DataFrame:

frame2=frame.set_index(['c','d']) frame2 Out[34]:      a b c  d    one 0 0 7   1 1 6   2 2 5 two 0 3 4   1 4 3   2 5 2   3 6 1

默认情况下,那些列会从DataFrame中移除,但也可以将其保留下来:

frame.set_index(['c','d'],drop=False) Out[35]:      a b  c d c  d        one 0 0 7 one 0   1 1 6 one 1   2 2 5 one 2 two 0 3 4 two 0   1 4 3 two 1   2 5 2 two 2   3 6 1 two 3

reset_index的功能和set_index刚好相反,层次化索引的级别会被转移到列里面:

frame2.reset_index() Out[36]:     c d a b 0 one 0 0 7 1 one 1 1 6 2 one 2 2 5 3 two 0 3 4 4 two 1 4 3 5 two 2 5 2 6 two 3 6 1

上述就是小编为大家分享的使用pandas怎么层次化索引了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI