MySQL數據優化-多層索引

一、多層索引

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數據優化-多層索引的文章就介紹到這瞭,更多相關數據優化-多層索引內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: