pandas 层次化索引的实现方法

作者:平淡才是真~~ 时间:2023-11-24 19:04:02 

层次化索引是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

来源:https://www.cnblogs.com/dataAnalysis/p/9329827.html

标签:pandas,层次化索引
0
投稿

猜你喜欢

  • Golang中的错误处理的示例详解

    2024-02-05 03:54:44
  • Python 忽略文件名编码的方法

    2021-06-11 15:16:55
  • mysql索引必须了解的几个重要问题

    2024-01-27 17:58:54
  • 基于python实现模拟数据结构模型

    2022-11-12 23:44:01
  • Django自定义分页效果

    2023-12-06 00:09:04
  • Python爬虫爬取杭州24时温度并展示操作示例

    2022-01-04 14:43:33
  • Python 切片索引越界的问题(数组下标越界)

    2023-09-11 23:00:30
  • 如何解决mysql重装失败方法介绍

    2024-01-19 10:52:05
  • python 已知三条边求三角形的角度案例

    2022-09-06 01:18:41
  • BootstrapTable+KnockoutJS相结合实现增删改查解决方案(三)两个Viewmodel搞定增删改查

    2024-04-28 09:36:56
  • python使用Flask操作mysql实现登录功能

    2024-01-21 02:20:00
  • 理解JavaScript中的事件 Event

    2008-03-19 11:16:00
  • Python基于域相关实现图像增强的方法教程

    2023-08-24 15:30:22
  • 手残删除python之后的补救方法

    2021-04-13 12:50:04
  • 解决usageerror: line magic function "%%time" not found问题

    2022-06-16 15:53:29
  • python函数局部变量用法实例分析

    2023-08-24 17:50:25
  • Python中的赋值、浅拷贝、深拷贝介绍

    2023-09-29 06:01:54
  • Go语言基础变量的声明及初始化示例详解

    2024-04-27 15:46:37
  • Python中用pyinstaller打包时的图标问题及解决方法

    2021-11-10 01:41:33
  • python爬取youtube视频的示例代码

    2021-10-02 16:25:26
  • asp之家 网络编程 m.aspxhome.com