无涯教程-Pandas 多重索引

67 阅读1分钟

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

示例:

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]])

参考链接

www.learnfk.com/pandas/pand…