Series索引的工作方式类似于NumPy数组的索引,不过Series的索引值不只是整数,如:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
obj=Series(np.arange(4),index=['a','b','c','d'])
obj=Series(np.arange(4),index=['a','b','c','d'])
obj
Out[10]:
a 0
b 1
c 2
d 3
dtype: int32
obj['b']
Out[11]: 1 obj[1]
Out[12]: 1 obj[2:4]
Out[13]:
c 2
d 3
dtype: int32 obj[['b','a','d']]
Out[14]:
b 1
a 0
d 3
dtype: int32 obj[[1,3]]
Out[15]:
b 1
d 3
dtype: int32

obj[obj<2]
Out[17]:
a 0
b 1
dtype: int32

#利用标签索引与普通的Python切片运算不同
#因为末端是包含的
obj['b':'c']=5 obj
Out[24]:
a 0
b 5
c 5
d 3
dtype: int32
 
DataFrame 进行索引其实就是获取一个或者多个列:
 
获取列:指定列名称即可
data=DataFrame(np.arange(16).reshape((4,4)),index=['Ohio','Colorado','Utah','New York'],columns=['one','two','three','four'])

data
Out[26]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 data['two']
Out[27]:
Ohio 1
Colorado 5
Utah 9
New York 13
Name: two, dtype: int32 data[['three','one']]
Out[28]:
three one
Ohio 2 0
Colorado 6 4
Utah 10 8
New York 14 12
获取行:
(1)通过切片或布尔型数组;
(2)通过布尔型DataFrame进行索引;
(3)在行上标签索引,引入索引字段ix,它可以通过NumPy式的标记法及轴标签从DataFrame中选取行和列的子集。

#切片获取行
data[:2]
Out[29]:
one two three four
Ohio 0 1 2 3
Colorado 4 5 6 7 #布尔型数组获取行
data[data['three']>5]
Out[30]:
one two three four
Colorado 4 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 #布尔型DataFrame进行索引
data<5
Out[31]:
one two three four
Ohio True True True True
Colorado True False False False
Utah False False False False
New York False False False False #将data<5的数值赋值为0
data[data<5]=0 data
Out[33]:
one two three four
Ohio 0 0 0 0
Colorado 0 5 6 7
Utah 8 9 10 11
New York 12 13 14 15 #行上进行标签索引,使用索引字段ix
data.ix['Colorado',['two','three']]
Out[34]:
two 5
three 6
Name: Colorado, dtype: int32 data.ix[['Colorado','Utah'],[3,0,1]]
Out[35]:
four one two
Colorado 7 0 5
Utah 11 8 9 #索引的是行索引号为2的数据,也就是行Utah
data.ix[2]
Out[36]:
one 8
two 9
three 10
four 11
Name: Utah, dtype: int32 data.ix[:'Utah','two']
Out[37]:
Ohio 0
Colorado 5
Utah 9
Name: two, dtype: int32 #索引data.three>5的行
data.ix[data.three>5,:3]
Out[38]:
one two three
Colorado 0 5 6
Utah 8 9 10
New York 12 13 14

DataFrame的索引选项

#选取DataFrame的单个列或者一组列
obj[val]
#选取的单个行或者一组行
obj.ix[val]
#选取单个列或列的子集
obj.ix[:,val]
#同时选取行和列
obj.ix[val1,val2]
 

最新文章

  1. Asp.Net Core--基于声明的授权
  2. iOS-----About Asset Catalog
  3. node 学习笔记 - path 处理
  4. map学习笔记
  5. 引水入城(codevs 1066)
  6. finally 语句
  7. gcj_2016_Round1_B
  8. Linux下使用mail命令发送邮件
  9. P2158 [SDOI2008]仪仗队 线性筛(欧拉函数和素数表)
  10. 关于 IIS 上的 Speech 设置
  11. JSTL标签库--核心标签库
  12. js冒泡排序,数组去重
  13. Redis Cluster(集群)
  14. javascript第一个作业之网页计算器
  15. JAVA调用外部安装7-Zip压缩和解压zip文件
  16. Mysql中 in or exists not exists not in区别 (网络整理)
  17. php之强制回调类型callable
  18. FragmentStatePagerAdapter和FragmentPagerAdapter区别
  19. 并发库应用之一 &amp; ThreadLocal实现线程范围的共享变量
  20. 20145322 《Java程序设计》第7周学习总结

热门文章

  1. Spring学习笔记(5)——IoC再度学习
  2. Inversion of Control 控制反转 有什么好处
  3. vue组件库的基本开发步骤
  4. 解决 使用migrations 执行update-database 出现System.InvalidOperationException: 实例失败的问题
  5. stdin stdout stderr - 标准 I/O 流
  6. TypeError: Object of type &#39;int32&#39; is not JSON serializable
  7. day11 python名称空间 作用域
  8. QList和QVector等容器的区别:(转)
  9. Largest Submatrix 3
  10. k8s集群搭建之一:基础环境