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

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

技术教程推荐

深入浅出gRPC -〔李林锋〕

如何设计一个秒杀系统 -〔许令波〕

DevOps实战笔记 -〔石雪峰〕

数据中台实战课 -〔郭忆〕

Service Mesh实战 -〔马若飞〕

Kafka核心源码解读 -〔胡夕〕

Serverless进阶实战课 -〔静远〕

B端体验设计入门课 -〔林远宏(汤圆)〕

AI绘画核心技术与实战 -〔南柯〕

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