📜  Pandas 多重索引

📅  最后修改于: 2020-10-29 03:15:32             🧑  作者: Mango

多重索引

多重索引被定义为非常重要的索引,因为它处理数据分析和操作,尤其是处理高维数据时。它还可以在Series和DataFrame等较低维度的数据结构中存储和处理任意数量的维度的数据。

它是标准索引对象的层次结构类似物,用于将轴标签存储在pandas对象中。也可以将其定义为元组数组,其中每个元组都是唯一的。可以从数组列表,元组数组和可迭代的交叉集创建。

例:

arrays = [['it', 'it', 'of', 'of', 'for', 'for', 'then', 'then'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
tuples

输出:

 [('it', 'one'),
 ('it', 'two'),
 ('of', 'one'),
 ('of', 'two'),
 ('for', 'one'),
 ('for', 'two'),
 ('then', 'one'),
 ('then', 'two')]

范例2:

arrays = [['it', 'it', 'of', 'of', 'for', 'for', 'then', 'then'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

输出:

MultiIndex([('bar', 'one'),
 [('it', 'one'),
 ('it', 'two'),
 ('of', 'one'),
 ('of', 'two'),
 ('for', 'one'),
 ('for', 'two'),
 ('then', 'one'),
 ('then', 'two')]
 names=['first', 'second'])

范例3:

import pandas as pd
import numpy as np
pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]], 
codes=[[0, -1, 1, 2, 3, 4]])

输出:

MultiIndex(levels=[[nan, None, NaT, 128, 2]],
           codes=[[0, -1, 1, 2, 3, 4]])