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)
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)
print(df.loc['G1'])
df.loc['G1'].loc[1]
df['A']
df.loc['G1'].loc[1]['A']
df.loc['G1']['A']
df
df.index.names
df.index.names = ['level','num']
df.index.names
df
df.xs(1, level='num')