pd.MultiIndex | |||
构造器 | MI | levels | 每个级别不重复的标签 |
labels | 每个级别的整数指定每个位置 | ||
*sortorder=None | |||
*names=None | 每个级别的name | ||
copy=False | |||
verify_integrity=True | 检查levels和labels |
.from_arrays() | MI | arrays ** | list_like: [[一级索引],[二级索引]], 每一级长度都必须跟索引长度相同 |
.from_product() | MI | iterable ** | [无重复值的迭代器], 排列组合生成长度为 len(1)*len(2)*... 的索引 |
.from_tuple() | MI | tuples ** | [(第n行索引, )], tuple的第一个元素为一级标签, ... |
In[38]:
import pandas as pd
m_index1=pd.MultiIndex(levels=[['A','B'],['x1','x2']],labels=[[0,1],[0,1]],name=[None,"class2"])
m_index1Out[38]:
MultiIndex(levels=[['A', 'B'], ['x1', 'x2']], labels=[[0, 1], [0, 1]], names=[None, 'class2'])In [43]: df1=pd.DataFrame(np.random.randint(1,10,(2,3)),index=m_index1)
df1Out[43]:
0 | 1 | 2 | ||
---|---|---|---|---|
class2 | ||||
A | x1 | 1 | 4 | 9 |
B | x2 | 4 | 9 | 6 |
class1=['A','A','B','B']
class2=['x1','x2','y1','y2']
pd.MultiIndex.from_arrays([class1,class2],names=['class1','class2'])Out[56]:
MultiIndex(levels=[['A', 'B'], ['x1', 'x2', 'y1', 'y2']], labels=[[0, 0, 1, 1], [0, 1, 2, 3]], names=['class1', 'class2'])In [57]:
pd.MultiIndex.from_product([['A','B'],['x1','y1']],names=['class1','class2'])Out[57]:
MultiIndex(levels=[['A', 'B'], ['x1', 'y1']], labels=[[0, 0, 1, 1], [0, 1, 0, 1]], names=['class1', 'class2'])