31.pandas MultiIndex

### Create a multi Index dataframe

Create a hierarichal index
Check each variable for understanding
create a multi index dataframe using randn, hierarichal index and columns
select the data outer index
select the data using outer and inner index
select the columns
select the data from the particular column in specific outer index
Check index.names
Assign index.names
Get the partiuclar value from the dataframe
cross section - xs  -- (indexname, levelname)
import pandas as pd
outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
hier_index = list(zip(outside,inside))
print(hier_index)
[('G1', 1), ('G1', 2), ('G1', 3), ('G2', 1), ('G2', 2), ('G2', 3)]

hier_index = pd.MultiIndex.from_tuples(hier_index)
from numpy.random import randn
df = pd.DataFrame(randn(6,2), hier_index, columns=['A','B'] )
print(df)
             A         B
G1 1  0.872359 -1.185877
   2  0.120721 -0.398765
   3  0.204190  2.519279
G2 1 -1.943766  0.402523
   2  0.893575 -0.451102
   3  1.519624  0.602376

print(df.loc['G1'])
          A         B
1  0.872359 -1.185877
2  0.120721 -0.398765
3  0.204190  2.519279

df.loc['G1'].loc[1]
A    0.872359
B   -1.185877
Name: 1, dtype: float64
df['A']
G1  1    0.872359
    2    0.120721
    3    0.204190
G2  1   -1.943766
    2    0.893575
    3    1.519624
Name: A, dtype: float64
df.loc['G1'].loc[1]['A']
0.8723594201586252
df.loc['G1']['A']
1    0.872359
2    0.120721
3    0.204190
Name: A, dtype: float64
df
A B
G1 1 0.872359 -1.185877
2 0.120721 -0.398765
3 0.204190 2.519279
G2 1 -1.943766 0.402523
2 0.893575 -0.451102
3 1.519624 0.602376
df.index.names
FrozenList([None, None])
df.index.names = ['level','num']
df.index.names
FrozenList(['level', 'num'])
df
A B
level num
G1 1 0.872359 -1.185877
2 0.120721 -0.398765
3 0.204190 2.519279
G2 1 -1.943766 0.402523
2 0.893575 -0.451102
3 1.519624 0.602376
df.xs(1, level='num')
A B
level
G1 0.872359 -1.185877
G2 -1.943766 0.402523