## Introduction to Python data analysis (7): Pandas hierarchical index

Squirrels love biscuits 2021-04-06 16:41:03
introduction python data analysis pandas

So let's create one Series, In the input index Index when , The input is made up of two subunits list Composed of list, The first one list It's the outer index , the second list It's the inner index .

Sample code ：

```import pandas as pd
import numpy as np
ser_obj = 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]
])
print(ser_obj)```

Running results ：

```a 0 0.099174
1 -0.310414
2 -0.558047
b 0 1.742445
1 1.152924
2 -0.725332
c 0 -0.150638
1 0.251660
2 0.063387
d 0 1.080605
1 0.567547
2 -0.154148
dtype: float64```

# MultiIndex Index object

• Print this Series The index type of , Display is MultiIndex

• Print the index directly , You can see that there is lavels, and labels Two messages .levels It represents the labels in the two levels ,labels It's what the label is for each location .

Sample code ：

```print(type(ser_obj.index))
print(ser_obj.index)```

Running results ：

```<class 'pandas.indexes.multi.MultiIndex'>
MultiIndex(levels=[['a', 'b', 'c', 'd'], [0, 1, 2]],
labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])```

# Select a subset

• Get data from index . Because now there are two layers of indexes , When getting data through the outer index , You can directly use the tag of the outer index to get .

• When you want to get data through the inner index , stay list Pass in two elements , The former refers to the outer index to be selected , The latter represents the inner index to be selected .

### 1. Outer layer selection ：

ser_obj['outer_label']

Sample code ：

```# Outer layer selection
print(ser_obj['c'])```

Running results ：

```0 -1.362096
1 1.558091
2 -0.452313
dtype: float64```

### 2. Inner layer selection ：

ser_obj[:, 'inner_label']

Sample code ：

```# Inner layer selection
print(ser_obj[:, 2])```

Running results ：

```a 0.826662
b 0.015426
c -0.452313
d -0.051063
dtype: float64```

Commonly used for grouping operations 、 The generation of pivot table, etc

# Switch the hierarchical order

### swaplevel()

.swaplevel( ) Exchange inner and outer indexes .

Sample code ：

`print(ser_obj.swaplevel())`

Running results ：

```0 a 0.099174
1 a -0.310414
2 a -0.558047
0 b 1.742445
1 b 1.152924
2 b -0.725332
0 c -0.150638
1 c 0.251660
2 c 0.063387
0 d 1.080605
1 d 0.567547
2 d -0.154148
dtype: float64```

# Swap and sort hierarchies

### sortlevel()

.sortlevel( ) Sort the outer index first , Then sort the inner index , The default is ascending .

Sample code ：

```# Swap and sort hierarchies
print(ser_obj.swaplevel().sortlevel())```

Running results ：

```0 a 0.099174
b 1.742445
c -0.150638
d 1.080605
1 a -0.310414
b 1.152924
c 0.251660
d 0.567547
2 a -0.558047
b -0.725332
c 0.063387
d -0.154148
dtype: float64```

https://pythonmana.com/2021/04/20210406163959530k.html