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. Nest.js 管道

    Docs: https://docs.nestjs.com/pipes 管道将输入数据转换为所需的输出.此外,它可以处理验证,当数据不正确时可能会抛出异常. 内置的 pipe import { Arg ...

  2. Access无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开

    使用SQL Server导入有密码的Access数据库内容,连接时出现错误提示: Access无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开 参考百度信息,可以点上图中的高级,在 ...

  3. JavaScript基础知识(函数)

    函数的基础 函数: 把实现相同功能的代码放到一个函数体中,当想实现这个功能时,直接执行这个函数即可:减少了的冗余:高内聚,低耦合--> 函数的封装: 函数:引用数据类型: var a = 10; ...

  4. JDBC笔记总结[申明:来源于网络]

    JDBC笔记总结[申明:来源于网络] 地址:http://blog.csdn.net/Summer_YuXia/article/details/53676386?ref=myread

  5. python摸爬滚打之day16----类的成员

    1.变量(字段) 实例变量(普通字段): 实例变量封装在对象中, 用的时候直接用对象来调用. 类变量(静态字段): 类变量封装在类中的, 同一个类不同对象都可以用, 用的时候直接用类名调用(对象也能调 ...

  6. CNPM 安装 for angularjs

    npm config set proxy=http://127.0.0.1:8087npm config delete proxynpm config set registry=http://regi ...

  7. python基础(11)-常用模块

    re(正则)模块 常用方法 findall() 以列表返回所有满足条件的结果 import re print(re.findall('\d','a1b2c2abc123'))#['1', '2', ' ...

  8. 把ResNet-L152模型的ckpt文件转化为pb文件

    import tensorflow as tf from tensorflow.python.tools import freeze_graph #os.environ['CUDA_VISIBLE_D ...

  9. iPhone IOS10安装APP没提示连接网络(无法联网)的解决办法

    iPhone升级ios10之后,遇到如标题所述问题时: 1.退出APP,设置-蜂窝移动网络-无线局域网助理-开启 2.进入APP,这时候就回提示连接网络了. 提醒: 数据流量有限的朋友,平时请关闭&q ...

  10. 关于.net里面的静态html页面和接口组合使用的网站

    在网站的根目录下,主要有三部分组成.①接口里面的bin文件夹②接口③html里面的页面. html里面有ajax请求接口的js代码.另外接口里面的web.config不需要拷贝到网站根目录去. 如下截 ...