import numpy as np import pandas as pd index=[("beijing",2007),("beijing", 2017), ("beijing", 2027),("shenzhen",2007), ("shenzhen",2017),("shenzhen",2027)] money=np.random.rand(6) Mindex=pd.MultiIndex.from_tuples(index) city=pd.DataFrame(money,index=Mindex) print(city) index=[(2007,"beijing"),(2017,"beijing"), (2027,"beijing"),(2007,"shenzhen"), (2017,"shenzhen"),(2027,"shenzhen")] Mindex2=pd.MultiIndex.from_tuples(index) print(city.reindex(Mindex2))#重构但是city不变 print(city.loc["beijing"])#取出某一个索引 print(city.iloc[0])#取出某一行 print(city.loc[["beijing","shenzhen"]])#切片 print(city.iloc[0:4])#取出0-4切片 print(city.loc[["beijing","shenzhen"]].iloc[0:3])#切片