对Pandas MultiIndex(多重索引)详解

yipeiwu_com5年前Python基础

创建多重索引

In [16]: df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)

In [17]: df
Out[17]: 
first  bar     baz     foo     qux \
second  one  two  one  two  one  two  one 
A  0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 -1.170299 
B  0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 
C  -1.413681 1.607920 1.024180 0.569605 0.875906 -2.211372 0.974466 

first    
second  two 
A  -0.226169 
B  -1.436737 
C  -2.006747 

获得索引信息

get_level_values

In [23]: index.get_level_values(0)
Out[23]: Index(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], dtype='object', name='first')

In [24]: index.get_level_values('second')
Out[24]: Index(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype='object', name='second')

基本索引

In [25]: df['bar']
Out[25]: 
second  one  two
A  0.895717 0.805244
B  0.410835 0.813850
C  -1.413681 1.607920

In [26]: df['bar', 'one']
Out[26]: 
A 0.895717
B 0.410835
C -1.413681
Name: (bar, one), dtype: float64

In [27]: df['bar']['one']
Out[27]: 
A 0.895717
B 0.410835
C -1.413681
Name: one, dtype: float64

使用reindex对齐数据

数据准备

In [11]: s = pd.Series(np.random.randn(8), index=arrays)

In [12]: s
Out[12]: 
bar one -0.861849
  two -2.104569
baz one -0.494929
  two 1.071804
foo one 0.721555
  two -0.706771
qux one -1.039575
  two 0.271860
dtype: float64

s序列加(0~-2)索引的值,因为s[:-2]没有最后两个的索引,所以为NaN.s[::2]意思是步长为1.

In [34]: s + s[:-2]
Out[34]: 
bar one -1.723698
  two -4.209138
baz one -0.989859
  two 2.143608
foo one 1.443110
  two -1.413542
qux one   NaN
  two   NaN
dtype: float64

In [35]: s + s[::2]
Out[35]: 
bar one -1.723698
  two   NaN
baz one -0.989859
  two   NaN
foo one 1.443110
  two   NaN
qux one -2.079150
  two   NaN
dtype: float64

以上这篇对Pandas MultiIndex(多重索引)详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python利用正则表达式提取字符串

前言 正则表达式的基础知识就不说了,有兴趣的可以点击这里,提取一般分两种情况,一种是提取在文本中提取单个位置的字符串,另一种是提取连续多个位置的字符串。日志分析会遇到这种情况,下面我会分...

对python中UDP,socket的使用详解

对python中UDP,socket的使用详解

讲到UDP和TCP之前咱们先了解一下socket Socket socket简称套接字,是进程间通信的一种方式。与其他的方式的进程间的通讯的方式不同的是,socket是实现了主机间进程间...

Python实现的一个找零钱的小程序代码分享

Python写的一个按面值找零钱的程序,按照我们正常的思维逻辑从大面值到小面值的找零方法,人民币面值有100元,50元,20元,10元,5元,1元,5角,1角,而程序也相应的设置了这些面...

发布你的Python模块详解

我们在学习Python的时候,除了用pip安装一些模块之外,有时候会从网站下载安装包下来安装,我也想要把我自己编写的模块做成这样的安装包,该怎么办,如何发布呢? 大概需要以下四个步骤:...

Python socket实现多对多全双工通信的方法

服务器:#server.py #!/usr/bin/env python #-*-coding:utf-8-*- import sys import struct#将字符串打包为二进...