MySQL数据优化-多层索引
程序员文章站
2022-03-24 10:49:38
目录一、多层索引1.创建2.设置索引的名称3.from_arrays( )-from_tuples()4.笛卡儿积方式二、多层索引操作1.series2.dataframe3.交换索引4.索引排序5....
一、多层索引
1.创建
环境:jupyter
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['上半年','上半年','下半年','下半年'], ['一季度','二季度','三季度','四季度']], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','白菜','牛肉','猪肉']]) display(a)
2.设置索引的名称
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['上半年','上半年','下半年','下半年'], ['一季度','二季度','三季度','四季度']], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','白菜','牛肉','猪肉']]) a.index.names=['年度','季度'] a.columns.names=['大类','小类'] display(a)
3.from_arrays( )-from_tuples()
import numpy as np import pandas as pd index=pd.multiindex.from_arrays([['上半年','上半年','下半年','下半年'],['一季度','二季度','三季度','四季度']]) columns=pd.multiindex.from_tuples([('蔬菜','胡萝卜'),('蔬菜','白菜'),('肉类','牛肉'),('肉类','猪肉')]) a=pd.dataframe(np.random.random(size=(4,4)),index=index,columns=columns) display(a)
4.笛卡儿积方式
from_product() 局限性较大
import pandas as pd index = pd.multiindex.from_product([['上半年','下半年'],['蔬菜','肉类']]) a=pd.dataframe(np.random.random(size=(4,4)),index=index) display(a)
二、多层索引操作
1.series
import pandas as pd a=pd.series([1,2,3,4],index=[['a','a','b','b'],['c','d','e','f']]) print(a) print('---------------------') print(a.loc['a']) print('---------------------') print(a.loc['a','c'])
import pandas as pd a=pd.series([1,2,3,4],index=[['a','a','b','b'],['c','d','e','f']]) print(a) print('---------------------') print(a.iloc[0]) print('---------------------') print(a.loc['a':'b']) print('---------------------') print(a.iloc[0:2])
2.dataframe
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['上半年','上半年','下半年','下半年'], ['一季度','二季度','三季度','四季度']], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','白菜','牛肉','猪肉']]) print(a) print('--------------------') print(a.loc['上半年','二季度']) print('--------------------') print(a.iloc[0])
3.交换索引
swaplevel( )
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['2021','2021','2022','2022'], ['一季度','二季度','三季度','四季度']], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','白菜','牛肉','猪肉']]) a.index.names=['年度','季度'] print(a) print('--------------------') print(a.swaplevel('年度','季度'))
4.索引排序
sort_index( )
-
level
:指定根据哪一层进行排序,默认为最层 -
inplace
:是否修改原数据。默认为false
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['2021','2021','2022','2022'], [1,3,2,4]], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','白菜','牛肉','猪肉']]) a.index.names=['年度','季度'] print(a) print('--------------------') print(a.sort_index()) print('--------------------') print(a.sort_index(level=1))
5.索引堆叠
stack( )
将指定层级的列转换成行
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['2021','2021','2022','2022'], [1,3,2,4]], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','胡萝卜','牛肉','牛肉']]) print(a) print('--------------------') print(a.stack(0)) print('--------------------') print(a.stack(-1))
6.取消堆叠
unstack( )
将指定层级的行转换成列
fill_value
:指定填充值。
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['2021','2021','2022','2022'], [1,3,2,4]], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','胡萝卜','牛肉','牛肉']]) print(a) print('--------------------') a=a.stack(0) print(a) print('--------------------') print(a.unstack(-1))
import numpy as np import pandas as pd a=pd.dataframe(np.random.random(size=(4,4)),index=[['2021','2021','2022','2022'], [1,3,2,4]], columns=[['蔬菜','蔬菜','肉类','肉类'],['胡萝卜','胡萝卜','牛肉','牛肉']]) print(a) print('--------------------') a=a.stack(0) print(a) print('--------------------') print(a.unstack(0,fill_value='0'))
到此这篇关于mysql数据优化-多层索引的文章就介绍到这了,更多相关数据优化-多层索引内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: MySQL索引失效原理
下一篇: 天凉赏冷段,凉爽又开心!