Pandas的标签处理需要分成多种情况来处理,Series和DataFrame根据标签索引数据的操作方法是不同的,单列索引和双列索引的操作方法也是不同的。

单列索引

In [2]: import pandas as pd

In [3]: import numpy as np

In [4]: df = pd.DataFrame(np.ones((2, 4)), index=list("AB"), columns=list("abcd"))

In [5]: df.iloc[0,0]=100

In [6]: df
Out[6]:
a b c d
A 100.0 1.0 1.0 1.0
B 1.0 1.0 1.0 1.0

reindex所插入的标签如果是原来的标签中没有的,就会将该行的值全部置为NaN

In [7]: df.reindex(["A", "f"])
Out[7]: ssss
a b c d
A 100.0 1.0 1.0 1.0
f NaN NaN NaN NaN In [8]: df
Out[8]:
a b c d
A 100.0 1.0 1.0 1.0
B 1.0 1.0 1.0 1.0

使用index修改标签

In [9]: df.index = ["a", "b"]

In [10]: df
Out[10]:
a b c d
a 100.0 1.0 1.0 1.0
b 1.0 1.0 1.0 1.0

使用set_index将某一列变为标签

In [11]: df.set_index("a")
Out[11]:
​ b c d
a
100.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 In [12]: df
Out[12]:
​ a b c d
a 100.0 1.0 1.0 1.0
b 1.0 1.0 1.0 1.0
# 使用drop参数控制将某一列作为索引后是否删除原数据
In [13]: df.set_index("a", drop=False)
Out[13]:
​ a b c d
a
100.0 100.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
# 使用unique函数可以去除重复值
In [14]: df.set_index("b", drop=False).index.unique()
Out[14]: Float64Index([1.0], dtype='float64', name='b') In [15]: df.set_index("b", drop=False).index
Out[15]: Float64Index([1.0, 1.0], dtype='float64', name='b') In [16]: len(df.set_index("b", drop=False).index.unique())
Out[16]: 1

双列索引

In [17]: df.set_index(["a","b"])
Out[17]:
c d
a b
100.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0
# levels这个列表中含有两个列表,分别是双列索引的每一列
In [18]: df.set_index(["a","b"]).index
Out[18]:
MultiIndex(levels=[[1.0, 100.0], [1.0]],
labels=[[1, 0], [0, 0]],
names=['a', 'b']) In [19]: a = pd.DataFrame({'a': range(7),'b': range(7, 0, -1),'c': ['one','one','one','two','two','two', 'two'],'d': list("hjklmno")}) In [20]: a
Out[20]:
a b c d
0 0 7 one h
1 1 6 one j
2 2 5 one k
3 3 4 two l
4 4 3 two m
5 5 2 two n
6 6 1 two o In [21]: b = a.set_index(["c","d"]) In [22]: b
Out[22]:
​ a b
c d
one h 0 7
​ j 1 6
​ k 2 5
two l 3 4
​ m 4 3
​ n 5 2
​ o 6 1 In [23]: c = b["a"] In [24]: c
Out[24]:
c d
one h 0
​ j 1
​ k 2
two l 3
​ m 4
​ n 5
​ o 6
Name: a, dtype: int64

双列索引取值

In [25]: c["two"]["l"]
Out[25]: 3 In [26]: c["one"]
Out[26]:
d
h 0
j 1
k 2
Name: a, dtype: int64 In [27]: d = a.set_index(["d","c"]) In [28]: d = d["a"] In [43]: d
Out[43]:
d c
h one 0
j one 1
k one 2
l two 3
m two 4
n two 5
o two 6
Name: a, dtype: int64
# 对于索引数少的列在后的情况,如果直接取会发生错误
In [44]: d["one"]
---------------------------------------------------------------------------
KeyError Traceback (most recent call
...
KeyError: 'one'

swaplevel()函数进行标签列换位

In [45]: d.swaplevel()
Out[45]:
c d
one h 0
​ j 1
​ k 2
two l 3
​ m 4
​ n 5
​ o 6
Name: a, dtype: int64 In [46]: d = d.swaplevel() In [47]: d["one"]
Out[47]:
d
h 0
j 1
k 2
Name: a, dtype: int64 In [48]: b
Out[48]:
​ a b
c d
one h 0 7
​ j 1 6
​ k 2 5
two l 3 4
​ m 4 3
​ n 5 2
​ o 6 1

对于DataFrame类型数组的双列索引,取值时应该加上loc或iloc

In [49]: b.loc["one"]
Out[49]:
a b
d
h 0 7
j 1 6
k 2 5 In [51]: d.loc["two"].loc["m"]
Out[51]: 4

Pandas之索引的更多相关文章

  1. pandas重置索引的几种方法探究

    pandas重置索引的几种方法探究 reset_index() reindex() set_index() 函数名字看起来非常有趣吧! 不仅如此. 需要探究. http://nbviewer.jupy ...

  2. (三)pandas 层次化索引

    pandas层次化索引 1. 创建多层行索引 1) 隐式构造 最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组 Series也可以创建多层索引 import numpy ...

  3. pandas 数据索引与选取

    我们对 DataFrame 进行选择,大抵从这三个层次考虑:行列.区域.单元格.其对应使用的方法如下:一. 行,列 --> df[]二. 区域   --> df.loc[], df.ilo ...

  4. pandas重新索引

    #重新索引会更改DataFrame的行标签和列标签.重新索引意味着符合数据以匹配特定轴上的一组给定的标签. #可以通过索引来实现多个操作 - #重新排序现有数据以匹配一组新的标签. #在没有标签数据的 ...

  5. pandas DataFrame 索引(iloc 与 loc 的区别)

    Pandas--ix vs loc vs iloc区别 0. DataFrame DataFrame 的构造主要依赖如下三个参数: data:表格数据: index:行索引: columns:列名: ...

  6. Pandas重建索引

    重新索引会更改DataFrame的行标签和列标签.重新索引意味着符合数据以匹配特定轴上的一组给定的标签. 可以通过索引来实现多个操作 - 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置 ...

  7. pandas层级索引1

    层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引. ...

  8. pandas层级索引

    层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引. ...

  9. python库学习笔记——Pandas数据索引:ix、loc、iloc区别

    Different Choices for Indexing 1. loc--通过行标签索引行数据 1.1 loc[1]表示索引的是第1行(index 是整数) import pandas as pd ...

随机推荐

  1. 方程式ETERNALBLUE 之fb.py的复现

    原文链接:https://www.t00ls.net/viewthread.php?tid=39343

  2. MUI - 复选框、单选框、使用js获取选择值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. 通过Navicat远程连接MySQL

    参考: http://blog.csdn.net/apple9005/article/details/53033148 问题一:在主机下通过Navicat连接服务器MySql的时候,提示“2003 C ...

  4. ubuntu16.04安装libzip库

    sudo apt install libzip-dev

  5. 格式化输出&初始编码&运算符

    一:格式化输出 %     %d   %s %为占位符   S替换的内容的类型为字符型 d替换的内容为整型 若在格式化输出的时候需要正常用到% 则表示时用两个%%表示 如: name = input( ...

  6. IDEA--生成jar包并且导出jar包

    PS:首先在idea中新建一个java文件,且带有main方法(不带有main好像不能导出,不确定) 参考文章:http://www.cnblogs.com/blog5277/p/5920560.ht ...

  7. springmvc.xml 中 <url-pattern></url-pattern>节点详解

    1.  先来上段常见的代码 <!-- MVC Servlet --> <servlet> <servlet-name>springServlet</servl ...

  8. javaweb(3)之JSP&EL&JSTL

    JSP(Java Server Page) 介绍 什么是 JSP ? 从用户角度看,JSP 就是一个网页. 从开发者角度看,它其实就是一个继承了 Servlet 的 java 类,所以可以直接说 JS ...

  9. vue的install

    将$platform挂载到Vue.prototype下面,this里面的内容就是$platform里面的东西 Vue.prototype.$platform = this 在chrome开发者工具下的 ...

  10. 580A

    #include <stdio.h> #include <string.h> #define MAXSIZE 100024//之前数据少开了一个量级 int money[MAX ...