Pandas hierarchical index 1

Source: Internet
Author: User
Hierarchical Indexing)

Create a series. When you input an Index, enter a list consisting of two sub-lists. The first sub-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=[                [‘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 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: float64

 

Multiindex index object
  • Print the index type of this series. The value is multiindex.

  • Print the index directly. You can see lavels and labels. Lavels indicates the labels that are exclusive to each level. Labels indicates the labels at each position.

Sample Code:

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

 

Running result:

<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 subset
  • Obtain data based on the index. Because there are two layers of indexes, when obtaining data through the outer index, you can directly obtain data using the outer index tag.

  • When you want to obtain data through the inner layer index, two elements are input in the list. The former indicates the outer layer index to be selected, and the latter indicates the inner layer index to be selected.

1. Outer selection:

Ser_obj ['outer _ label']

Sample Code:

# Print (ser_obj ['C']) for outer layers

 

Running result:

0   -1.3620961    1.5580912   -0.452313dtype: float64

 

2. Select the inner layer:

Ser_obj [:, 'inner _ label']

Sample Code:

# Print (ser_obj [:, 2]) for the inner layer

 

Running result:

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

 

It is often used for grouping operations and the generation of pivot tables.

Exchange layered order
1. swaplevel ()

. Swaplevel () exchanges the inner and outer indexes.

Sample Code:

print(ser_obj.swaplevel())

 

Running result:

0  a    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 Layers
Sortlevel ()

. Sortlevel () First sorts the external layer indexes and then the inner layer indexes. The default value is ascending.

Sample Code:

# Exchanging and sorting Layers
print(ser_obj.swaplevel().sortlevel())

 

Running result:

0  a    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

 

Pandas hierarchical index 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.