Pandas 多重索引

Pandas 多重索引 首页 / Pandas入门教程 / Pandas 多重索引

多重索引被定义为非常重要的索引,因为它处理数据分析和操作,尤其是处理高维数据时。它还可以在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]])

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

持续交付36讲 -〔王潇俊〕

零基础学Java -〔臧萌〕

小马哥讲Spring核心编程思想 -〔小马哥〕

跟月影学可视化 -〔月影〕

分布式数据库30讲 -〔王磊〕

如何讲好一堂课 -〔薛雨〕

快速上手C++数据结构与算法 -〔王健伟〕

结构执行力 -〔李忠秋〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)