Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the multi-tier index in MySQL data optimization?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how the multi-tier index in MySQL data optimization is like. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1. Multi-tier index

1. Create

Environment: Jupyter

Import numpy as npimport pandas as pda=pd.DataFrame (size= (4P4)), index= [['first half', 'first half', 'second half', 'second half'], ['first quarter', 'second quarter', 'third quarter', 'fourth quarter'], columns= [['vegetables', 'vegetables', 'meat'] 'meat'], ['carrots', 'cabbage', 'beef', 'pork']) display (a)

two。 Set the name of the index

Import numpy as npimport pandas as pda=pd.DataFrame (size= (4P4)), index= [['first half', 'first half', 'second half', 'second half'], ['first quarter', 'second quarter', 'third quarter', 'fourth quarter'], columns= [['vegetables', 'vegetables', 'meat'] 'meat'], ['carrots', 'cabbage', 'beef', 'pork']) a.index.names= ['year', 'quarter'] a.columns.namesame = ['big category', 'subclass'] display (a)

3.from_arrays ()-from_tuples ()

Import numpy as npimport pandas as pdindex=pd.MultiIndex.from_arrays (['first half', 'first half', 'second half'), ['first quarter', 'second quarter', 'third quarter', 'fourth quarter']) columns=pd.MultiIndex.from_tuples ([('vegetables', 'carrots'), ('vegetables', 'cabbage'), ('meat', 'beef'), ('meat' ]) a=pd.DataFrame (np.random.random (size= (4)), index=index,columns=columns) display (a)

4. Cartesian product mode

From_product () has more limitations.

Import pandas as pdindex = pd.MultiIndex.from_product ([['first half', 'second half'], ['vegetables', 'meat']) a=pd.DataFrame (np.random.random (size= (4Power4)), index=index) display (a)

Second, multi-tier index operation

1.Series

Import pandas as pda=pd.Series, index= [['a''], ['a'') print ('-') print (a.loc ['a']) print ('-') print (a.loc ['a'') 'c'])

Import pandas as pda=pd.Series ([1pyrum 2pyrm 3pyrm 4], index= ['axiajukkyo], [' cedronomy dcurries']. 'f']) print (a) print ('-') print (a.iloc [0]) print ('-') print (a.loc ['a') print ('-') print (a.iloc [0:2])

2.DataFrame

Import numpy as npimport pandas as pda=pd.DataFrame (size= (4P4)), index= [['first half', 'first half', 'second half', 'second half'], ['first quarter', 'second quarter', 'third quarter', 'fourth quarter'], columns= [['vegetables', 'vegetables', 'meat'] 'meat'], ['carrots', 'cabbage', 'beef', 'pork']) print (a) print ('-') print (a.loc ['first half', 'second quarter]) print (' -') print (a.iloc [0])

3. Swap index

Swaplevel ()

Import numpy as npimport pandas as pda=pd.DataFrame (size= (4P4)), index= [['2021', '2021', '2021', '2022'], ['first quarter', 'second quarter', 'third quarter', 'fourth quarter'], columns= [['vegetables', 'vegetables', 'meat', 'meat'] ['carrots', 'cabbage', 'beef', 'pork']) a.index.names= ['year', 'quarter'] print (a) print ('-') print (a.swaplevel ('year', 'quarter'))

4. Index sort

Sort_index ()

Level: specifies which layer to sort by, and defaults to the most layer

Inplace: whether to modify the original data. Default is False

Import numpy as npimport pandas as pda=pd.DataFrame (np.random.random (size= (4P4)), index= [['2021', '2021', '2021,' 2022, '2022], [1, 3, 10, 2, 5], columns= [[' vegetables', 'vegetables', 'meat', 'meat'], ['carrots', 'cabbage', 'beef' ]) a.index.namespace = ['year', 'quarter'] print (a) print ('-') print (a.sort_index) print ('-') print (a.sort_index (level=1))

5. Index stack

Stack ()

Converts columns at the specified level to rows

Import numpy as npimport pandas as pda=pd.DataFrame (np.random.random (size= (4P4)), index= [['2021', '2021', '2021,' 2022, '2022], [1, 3, 2, 5], columns= [[' vegetables', 'vegetables', 'meat', 'meat'], ['carrots', 'carrots', 'beef' ]) print (a) print ('-') print (a.stack (0)) print ('-') print (a.stack (- 1))

6. Unstacking

Unstack ()

Converts rows at the specified level to columns

Fill_value: specifies the padding value.

Import numpy as npimport pandas as pda=pd.DataFrame (np.random.random (size= (4P4)), index= [['2021', '2021', '2021,' 2022, '2022], [1, 3, 2, 5], columns= [[' vegetables', 'vegetables', 'meat', 'meat'], ['carrots', 'carrots', 'beef' ]) print (a) print ('-') a=a.stack (0) print (a) print ('-') print (a.unstack (- 1))

Import numpy as npimport pandas as pda=pd.DataFrame (np.random.random (size= (4P4)), index= [['2021', '2021', '2021,' 2022, '2022], [1, 3, 2, 5], columns= [[' vegetables', 'vegetables', 'meat', 'meat'], ['carrots', 'carrots', 'beef' 'beef']) print (a) print ('-') a=a.stack (0) print (a) print ('-') print (a.unstack)

This is how the multi-tier index in the MySQL data optimization shared by the editor is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report