<jupyter_start><jupyter_text>Topics covered in this chapter:NORMAL DISTRIBUTION1. zip()2. pd.MultiIndex()3. random from numpy4. df.loc[]5. .index6. .index.names7. .xs 8. .set_index() to set multiple indices9. .swap_level()10. .from_arrays    .from_tuples    .from_product 11. .sort_index()  1. Multi-indexing:<jupyter_code>outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
higher_index = list(zip(outside,inside))
higher_index
type(higher_index)
higher_index = pd.MultiIndex.from_tuples(higher_index)
higher_index

type(higher_index)<jupyter_output><empty_output><jupyter_text>2. np.round(matrix_name/any_number,round_till_this_digit) method:  [ np.random.seed(..) ]<jupyter_code>from numpy.random import randn as rn
#rn is the alias for the random given at the starting of this class (check Day 20)

np.random.seed(101)
df1 = pd.DataFrame(data = np.round(rn(6,3),2),index = higher_index,columns = ['A','B','C'])

df1
#CHECK THE .round method:

np.random.seed(101)
df2 = pd.DataFrame(data = np.round(rn(6,3),5),index = higher_index,columns = ['A','B','C'])

df2
pd.__version__<jupyter_output><empty_output><jupyter_text>3. Indexing and slicing:<jupyter_code>df1.loc['G1']
df1.loc['G2']
df1.loc['G1'].loc[[1,3],['A','C']]
df2.loc['G2'].loc[[2],['B']]
df1.loc['G1'].loc[[1,3]][['A','C']]
df1<jupyter_output><empty_output><jupyter_text>4. Giving the names to the outside and inside indices:<jupyter_code>df1.index.names = ["outside","inner"]
df1
outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
higher_index = list(zip(outside,inside))
print(higher_index)
higher_index = pd.MultiIndex.from_tuples(higher_index)
higher_index
type(higher_index)
from numpy.random import randn as rn
df = pd.DataFrame(data = np.round(rn(6,3),2), index = higher_index, columns = ['A','B','C'])
df
df.loc['G1'].loc[[1,3],['A','C']]
df.index.names = ["outer","inner"]
df
df.loc['G1']<jupyter_output><empty_output><jupyter_text>5. Cross -Section to get the data (for outer indices):    [ .xs('Name') ]  To extract the data of G1 only:<jupyter_code>df.xs('G1')<jupyter_output><empty_output><jupyter_text>6. Cross - Section to get the data for inner indices:   To extract the data of inner index 2 for both the outer indices:<jupyter_code>df.xs(2,level = "inner")<jupyter_output><empty_output><jupyter_text>7. Using set_index to set multiple indices:<jupyter_code>raw_data={'regiment':['Nighthawks','Nighthawks','Nighthawks','Nighthawks','Dragoons','Dragoons','Dragoons','Dragoons','Scouts','Scouts','Scouts','Scouts'],
         'company':['1st','1st','2nd','2nd','1st','1st','2nd','2nd','1st','1st','2nd','2nd'],
         'name':['Miller','Jacobson','Ali','Milner','Cooze','Jacon','Ryaner','Sone','Sloan','Piger','Riani','Ali'],
          'preTestScore':[4,24,31,2,3,4,24,31,2,3,2,3],
          'postTestScore':[25,94,57,62,70,25,94,57,62,70,62,70]}
df=pd.DataFrame(raw_data,columns=['regiment','company','name','preTestScore','postTestScore'])
df
df.set_index(['regiment','company'])
df.set_index(['regiment','company'],inplace=True)
df
df.index<jupyter_output><empty_output><jupyter_text>8. To interchange the levels of indices:   Swap inner and outer indices:   [   .swaplevel ]<jupyter_code>df.swaplevel('regiment','company')
df.sum(level='regiment')<jupyter_output><empty_output><jupyter_text>.from_arrays:<jupyter_code>#class method constructor
pd.MultiIndex.from_arrays([['a','a','b','b'],[1,2,1,2]])<jupyter_output><empty_output><jupyter_text>.from_tuples:<jupyter_code># list of tuples
pd.MultiIndex.from_tuples([('a',1),('a',2),('b',1),('b',2)])<jupyter_output><empty_output><jupyter_text>.from_product:<jupyter_code>#Cartesian product of single indices
pd.MultiIndex.from_product([['a','b'],[1,2]])<jupyter_output><empty_output><jupyter_text>levels and codes:<jupyter_code>pd.MultiIndex(levels=[['a','b'],[1,2]],
             codes=[[0,0,1,1],[0,1,0,1]])
# hierarchical indices and columns
index=pd.MultiIndex.from_product([[2013,2014],[1,2]],
                                names=['year','visit'])
columns=pd.MultiIndex.from_product([['Bob','Guido','Sue'],['HR','Temp']],
                                  names=['subject','type'])
data=np.round(np.random.randn(4,6),1)
health_data=pd.DataFrame(data,index=index,columns=columns)
health_data
#data[:,::2]*=10
data+=37
health_data=pd.DataFrame(data,index=index,columns=columns)
health_data
health_data['Guido','HR']
health_data.iloc[:2,:2]
health_data.loc[:,('Bob','HR')]<jupyter_output><empty_output><jupyter_text>[   .index.names  ]:<jupyter_code>index=pd.MultiIndex.from_product([['b','a','c'],[1,2]])
data=pd.Series(np.random.rand(6),index=index)
data.index.names=['char','int']
data
try:
    data['a':'b']
except KeyError as e:
    print(type(e))
    print(e)
data['a':'b']<jupyter_output><empty_output><jupyter_text>[     .sort_index()    ]<jupyter_code>data=data.sort_index()
data
data['a':'b']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21103166496424924, 'fraction_numerical': 0.038202247191011236, 'mean_word_length': 7.696269982238011, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '381464', 'license': 'MIT', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1929, 'n_words': 255, 'path': 'Data-Science-HYD-2k19/Topic-Wise/PANDAS/05. MULTI-LEVEL INDEXING.ipynb', 'repo_name': 'Sanjay9921/Python', 'size': '4895'}