Java教程

05-Pandas层级索引

本文主要是介绍05-Pandas层级索引,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.层级索引

import numpy as np
import pandas as pd
s1 = pd.Series(np.random.randn(12),index = [['a','a','a','b','b','b','c','c','c','d','d','d'],[0,1,2,0,1,2,0,1,2,0,1,2]])
s1
a  0    1.766538
   1    0.345160
   2   -1.049204
b  0   -0.212596
   1    0.220403
   2   -1.001067
c  0   -0.471350
   1    0.823931
   2   -0.551105
d  0   -0.190322
   1    0.701434
   2   -0.571929
dtype: float64
print(type(s1.index))
print(s1.index)
<class 'pandas.core.indexes.multi.MultiIndex'>
MultiIndex([('a', 0),
            ('a', 1),
            ('a', 2),
            ('b', 0),
            ('b', 1),
            ('b', 2),
            ('c', 0),
            ('c', 1),
            ('c', 2),
            ('d', 0),
            ('d', 1),
            ('d', 2)],
           )

2.选取

#1.外层选取
s1['b']
0   -0.212596
1    0.220403
2   -1.001067
dtype: float64
#2.内层获取
s1[:,2]
a   -1.049204
b   -1.001067
c   -0.551105
d   -0.571929
dtype: float64
s1['a',0]
1.766537562273998

3.交换

#1.swaplevel()交换内层和外层的索引
s1.swaplevel()
0  a    1.766538
1  a    0.345160
2  a   -1.049204
0  b   -0.212596
1  b    0.220403
2  b   -1.001067
0  c   -0.471350
1  c    0.823931
2  c   -0.551105
0  d   -0.190322
1  d    0.701434
2  d   -0.571929
dtype: float64
#2.sortlevel()先对外层索引进行排序,在对内层索引进行排序, 默认升序
s1.sortlevel()   # 此方法现在好像不支持了,需要使用 sort_index()方法,
D:\Anaconda\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: sortlevel is deprecated, use sort_index(level=...)
  





a  0   -0.241259
   1    0.898846
   2   -0.233832
b  0    0.796350
   1    0.025104
   2    1.232659
c  0   -0.899175
   1    0.684583
   2    0.970989
d  0   -0.075916
   1   -1.913964
   2    0.223474
dtype: float64
#交换并排序分层
s1.swaplevel().sortlevel()   # 这个也是不行了
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-11-413a6bd8d6a7> in <module>
      1 #交换并排序分层
----> 2 s1.swaplevel().sortlevel()


d:\python\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5139             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5140                 return self[name]
-> 5141             return object.__getattribute__(self, name)
   5142 
   5143     def __setattr__(self, name: str, value) -> None:


AttributeError: 'Series' object has no attribute 'sortlevel'

                    
这篇关于05-Pandas层级索引的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!