多的不说,直接看代码,有注释!
import pandas as pd
import numpy as np
# 层次化索引
# 创建一个 Series 数据结构
data = [17, 20, 21, 18, 19, 22]
s = pd.Series(data, index=[["first", "first", "second", "second", "third", "third"], ["boy", "girl", "boy", "girl", "boy", "girl"]])
print(s)
print(s.index) # 显示层次化索引
data = np.random.randint(10, 20, (6, 9)) # [10, 20)之间的随机整数
index = [["first", "first", "second", "second", "third", "third"], ["boy", "girl", "boy", "girl", "boy", "girl"]]
columns = [["Ch", "Ch", "Ch", "Math", "Math", "Math", "En", "En", "En"], ["A", "A", "B", "B", "C", "C", "A", "B", "C"]]
d_1 = pd.DataFrame(data, index=index, columns=columns) # 对于DataFrame来说,行和列都能进行层次化索引,也就是四个索引决定一个值,将一个二维数据变成了一个四维数据
print(d_1)
print(d_1.index)
print(d_1.columns)
print(s["first"]) # 传入第一个索引
print(s["first": "second"]) # 获取first和second的男女生人数
print(s[["first", "third"]]) # 可以多选
print(s["first", "boy"]) # 获取first的男生人数
print(s[:, "girl"]) # 获取女生总人数
print(s["first"]["boy"]) # 可以先选择外层索引再选择内存索引
print(s["first": "third"][:, "girl"])
print(d_1["Math"]) # 获取所有学生的math成绩各等级人数
print(d_1.loc["first"]) # 获取first的所有项
print(d_1.loc["first", "En"]) # 获取first男女生英语成绩各等级的人数
print(d_1["Ch"]["A"]) # 获取所有人语文成绩为A的人数
print(d_1.loc["first", ["Ch", "En"]]) # 获取first男女生的语文和英语成绩各等级人数
print(d_1.loc["first", "Ch"]["A"]) # 获取first男女生的语文成绩A等级人数:
print(d_1.loc["first", :].loc["boy"]) # 获取first男生的各科成绩等级人数