SoFunction
Updated on 2024-11-14

New approach to hierarchical indexing and fetching of pandas explained in detail

1、Hierarchical index

1.1 Definitions

Having multiple (two and more) index levels in a particular direction is called a hierarchical index.

With hierarchical indexing, pandas is able to handle high latitude data in a lower dimensional form

With hierarchical indexing, you can count data by level

Hierarchical indexes include Series hierarchical indexes and DataFrame hierarchical indexes.

1.2 Hierarchical Indexing of Series

import numpy as np
import pandas as pd

s1 = (data=[99, 80, 76, 80, 99],
    index=[['2017', '2017', '2018', '2018', '2018'], ['Eman Zhang', 'Zhang Qiaoling', 'Sissy Cheung', 'Cecilia Cheung', 'Coco Zhang']])
print(s1)

pandas 层次索引与取值

1.3 Hierarchical Indexing of DataFrame

# Hierarchical indexing for DataFrame
df1 = ({
 'year': [2016, 2016, 2017, 2017, 2018],
 'fruit': ['apple', 'banana', 'apple', 'banana', 'apple'],
 'production': [10, 30, 20, 70, 100],
 'profits': [40, 30, 60, 80,10],
})
print("df1===================================")
print(df1)

df2 = df1.set_index(['year', 'fruit'])
print("df2===================================")
print(df2)

print("===================================")
print()

print("(level='year')===================================")
print((level='year'))

print("(level='fruit')===================================")
print((level='fruit'))

print("(level=['year', 'fruit'])===================================")
print((level=['year', 'fruit']))

pandas 层次索引与取值

pandas 层次索引与取值

2. A new approach to taking values

ix is the older way. The new way is to use iloc loc.

iloc operates on subscripted values Both Series and DataFrames can be operated on.

loc Operates on indexed values Both Series and DataFrames can be operated on.

2.1 Series

# # A new way of taking values
s1 = (data=[99, 80, 76, 80, 99],
    index=[['2017', '2017', '2018', '2018', '2018'], ['Eman Zhang', 'Zhang Qiaoling', 'Sissy Cheung', 'Cecilia Cheung', 'Coco Zhang']])

print("s1=================================")
print(s1)

print("[2]=================================")
print([2])

print("['2018']['Cecilia Cheung']=================================")
print(['2018']['Cecilia Cheung'])

pandas 层次索引与取值

2.2 DataFrame

df1 = ({
 'year': [2016, 2016, 2017, 2017, 2018],
 'fruit': ['apple', 'banana', 'apple', 'banana', 'apple'],
 'production': [10, 30, 20, 70, 100],
 'profits': [40, 30, 60, 80,10],
})
print("df1===================================")
print(df1)

print("Old method to get the value===================================")
print("df1['year'][0]===================================")
print(df1['year'][0])

print("[0]['year']===================================")
print([0]['year'])

print("New method to get the value===================================")
print("[0][3]===================================")
print([0][3])

print("[0]['year']===================================")
print([0]['year'])

pandas 层次索引与取值

Above this new method of pandas hierarchical indexing and taking values in detail is all I have to share with you, I hope to be able to give you a reference, and I hope you will support me more.