In [14]:
import pandas as pd
import numpy as np
from numpy.random import randn
np.random.seed(3)
m1=randn(6,2)
In [6]:
outside = ['G1','G1','G1','G2','G2','G2']
inside = [1,2,3,1,2,3]
hier_index = list(zip(outside,inside))
In [7]:
hier_index
Out[7]:
In [9]:
hier_index1 = pd.MultiIndex.from_tuples(hier_index)
In [19]:
hier_index1
Out[19]:
In [15]:
df = pd.DataFrame(m1,index=hier_index1,columns=['A','B'])
In [20]:
df
Out[20]:
In [21]:
df.loc['G1']
Out[21]:
In [18]:
df.loc['G1'].loc[1]
Out[18]:
In [23]:
df.index.names=["gr","nm"]
In [24]:
df
Out[24]:
In [27]:
df.xs(["G1",1])
Out[27]:
In [30]:
df.xs(1,level='nm')
Out[30]:
No comments:
Post a Comment