pandas dataframe多层索引取值

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:
pandas dataframe多层索引取值

行索引取值

# 取外层索引为'class1'的数据
df.loc['class1']
pandas dataframe多层索引取值
# 同时根据多个索引筛选取值,法一:
df.loc[('class2', 'HanMeiMei')]
pandas dataframe多层索引取值
# 同时根据多个索引筛选取值,法二:
df.loc['class2'].loc['HanMeiMei']
pandas dataframe多层索引取值
# 取内层索引:
# 先交换内外层索引位置
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']
pandas dataframe多层索引取值

列索引取值

df数据:

pandas dataframe多层索引取值
# 外层列索引:
df['math']
pandas dataframe多层索引取值
# 根据多层索引联合取值:
# 以下4句代码等效:
df['math','term2']
df.loc[:, ('math','term1')]
df['math']['term2']
df[('math','term1')]
pandas dataframe多层索引取值
# 与行索引类似,取内层索引先交换轴
df.swaplevel(axis=1)
pandas dataframe多层索引取值
df.swaplevel(axis=1)['term1']
pandas dataframe多层索引取值
上一篇:jquery选择器常见操作


下一篇:instanceof与isAssignableFrom的区别