Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Pandas hierarchical index

Shulou Source: shulou.com Published: 2022-06-03 05:27:06 10月04日 Update

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

Tags: Index inner layer outer layer code example result run two sort data label hierarchy time input size location information element ascending order subset Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Huawei Docker NVidia