pandas dataframe多层索引取值

人工智能89
import pandas as pd
import numpy as np

# 新建df数据
df = pd.DataFrame(np.random.randint(50, 100, size=(4, 4)),
                 columns=pd.MultiIndex.from_product(
                 [['math', 'physics'], ['term1', 'term2']]),
                 index=pd.MultiIndex.from_tuples(
                 [('class1', 'LiLei'), ('class2', 'HanMeiMei'),
                 ('class2', 'LiLei'), ('class2', 'HanMeiMei')]))
df.index.names = ['class', 'name']
# >>输出df:

行索引取值

# 取外层索引为'class1'的数据
df.loc['class1']
# 同时根据多个索引筛选取值,法一:
df.loc[('class2', 'HanMeiMei')]
# 同时根据多个索引筛选取值,法二:
df.loc['class2'].loc['HanMeiMei']
# 取内层索引:
# 先交换内外层索引位置
df.swaplevel()
# 输出:
            math                            physics
            term1           term2           term1         term2
name        class
LiLei       class1  81      81      77      91
HanMeiMei   class2  82      83      84      79
LiLei       class2  78      50      81      64
HanMeiMei   class2  59      94      89      52

# 再通过取外层索引的方法取值
df.swaplevel().loc['HanMeiMei']

列索引取值

df数据:

# 外层列索引:
df['math']
# 根据多层索引联合取值:
# 以下4句代码等效:
df['math','term2']
df.loc[:, ('math','term1')]
df['math']['term2']
df[('math','term1')]
# 与行索引类似,取内层索引先交换轴
df.swaplevel(axis=1)
# 交换轴后取外层列索引即可
df.swaplevel(axis=1)['term1']

Original: https://www.cnblogs.com/jaysonteng/p/13475618.html
Author: 邓安君
Title: pandas dataframe多层索引取值