python 层次化索引_pandas 层次化索引的实现方法

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DataFrame的层次化索引是指在DataFrame中的行或列上使用多个索引级别来表示数据的一种结构。这种索引结构可以让我们在一个轴上拥有多个索引级别。 在构建DataFrame的层次化索引时,可以通过多种方法实现。一种方法是在导入数据时直接使用列名作为行索引,使用`index_col`参数来指定多个列作为索引。另一种方法是使用`set_index()`方法将某几列设置为索引。设置索引后,可以选择是否保留原始列作为DataFrame的一部分。而要将层次化索引转换为二维表格形式,可以使用`reset_index()`函数。 除了以上方法,还可以在创建DataFrame时直接指定多级索引,可以通过使用一个由列表或数组组成的列表作为索引实现。例如,可以使用`pd.DataFrame()`函数的`index`参数来指定多级索引的值。 综上所述,DataFrame的层次化索引可以通过在导入数据时设置索引列,使用`set_index()`方法或在创建DataFrame时直接指定多级索引实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python学习笔记8——Series与DataFrame中的层次化索引](https://blog.csdn.net/weixin_44181744/article/details/105977700)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [pandas 层次化索引实现方法](https://download.csdn.net/download/weixin_38679651/14914102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值