mask=pd.MultiIndex.from_arrays([['Ohio', 'Ohio', 'Colorado'], ['Green','bed', 'Green']], names=('state', 'color')) df = pd.DataFrame(np.arange(12).reshape((4, 3)),index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]], columns=mask) df.index.names=['key1','key2'] df.columns.names=['state','color'] print(" 源数据: ",df) print(" 对源数据index排序: ",df.sort_index(level=1)) print(" 交换级别: ") print(df.swaplevel('key1','key2')) print(" 交换级别后排序: ") print(df.swaplevel('key1','key2').sort_index(level=0))
结果:
可以对比看到,排序的结果是一样的
END
#df.set_index( )函数的用法 df=pd.DataFrame({'a':range(7),'b':range(7,0,-1),'c':['one','one','one','two','two','two','two'], 'd':[0,1,2,0,1,2,3]}) print("源数据: ",df) df2=df.set_index(['c','d']) df3=df.set_index(['c','d'],drop=False) print(" df.set_index(['c','d']层次化索引,df2: ",df2) print(" df.set_index(['c','d'],drop=False层次化索引,drop,df3: ",df3)
结果:
END
left1 = pd.DataFrame({'key': ['a', 'b', 'a', 'a', 'b','c'],'value': range(6)}) right1 = pd.DataFrame({'group_val': [3.5, 7]}, index=['a', 'b']) print("left1: ",left1) print("right1: ",right1) mask=left1.join(right1) mask2=left1.join(right1,on='key') print(" 不指定key值: ",mask) print(" 指定key值: ",mask2)
结果:
END