Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Pandas hierarchical index

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Create a Series under the hierarchical index (hierarchical indexing). When you enter the index Index, you enter a list consisting of two child list

The first child list is the outer index and the second list is the inner index.

Sample code:

Import pandas as pdimport numpy as npser_obj = pd.Series (np.random.randn (12), index= [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 1, 2, 0, 1, 2, 0, 1) 2]]) print (ser_obj)

Running result:

A 0 0.099174 1-0.310414 2-0.558047b 0 1.742445 1 1.152924 2-0.725332c 0-0.150638 1 0.251660 2 0.063387d 0 1.080605 1 0.567547 2-0.154148dtype: float64MultiIndex index object prints the Series index type, showing that MultiIndex prints the index directly, you can see levels and labels information. Levels indicates which tags are in the two levels, and what is the label of each location in labels.

Sample code:

Print (type (ser_obj.index)) print (ser_obj.index)

Running result:

MultiIndex (levels= [['await,' baked, 'cased,' d'], [0,1,2]], labels= [[0,0,0,1,1,2,2,2,3,3,3], [0,1,2,0,1,2,0]) selects a subset to obtain data according to the index. Because there are now two-tier indexes, when you get data through the outer index, you can directly use the label of the outer index to get it. When you want to get the data through the inner index, pass in two elements in the list, the former represents the outer index to be selected, and the latter represents the inner index to be selected. 1. Outer layer selection: ser_obj ['outer_label']

Sample code:

# Select print (ser_obj ['c']) in the outer layer

Running result:

0-1.3620961 1.5580912-0.452313dtype: float642. Inner layer selection: ser_obj [:, 'inner_label']

Sample code:

# Select print (ser_obj [:, 2]) in the inner layer

Running result:

A 0.826662b 0.015426c-0.452313d-0.051063dtype: float64 is often used to exchange hierarchical order such as packet operation, PivotTable generation and so on. Swaplevel (). Swaplevel () exchanges inner and outer indexes.

Sample code:

Print (ser_obj.swaplevel ())

Running result:

0a 0.0991741 a-0.3104142 a-0.5580470 b 1.7424451 b 1.1529242 b-0.7253320 c-0.1506381 c 0.2516602 c 0.0633870 d 1.0806051 d 0.5675472 d-0.154148dtype: float64 exchange and sort hierarchical sort_index (). Sort_index () sorts the outer index first, and then the inner index. The default is ascending order.

Sample code:

# Exchange and sort hierarchical print (ser_obj.swaplevel (). Sort_index ())

Running result:

0a 0.099174 b 1.742445 c-0.150638 d 1.0806051 a-0.310414 b 1.152924 c 0.251660 d 0.5675472 a-0.558047 b-0.725332 c 0.063387 d-0.154148dtype: float64

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report