在数据科学领域python逐渐火热起来,超越了原有R的地位,这里丰富的第三方包的贡献功不可没,数值计算中Numpy和Pandas绝对是必备的神器,最近使用到Pandas来做数据的操作,今天正好有时间就简单地总结记录一点自己学习使用Pandas的体会,主要是对几个主要的数据框索引函数进行讲解和使用,相关的解释都已经在代码里面了,就不再多解释了,配合着我给出来的例子相信还是很容易理解的,下面是具体的实践:

#!usr/bin/env python
#encoding:utf-8
from __future__ import division'''
__Author__:沂水寒城
功能:Pandas数据框索引函数 iloc、loc和ix学习使用
''''''
Pandas库中有iloc和loc以及ix可以用来索引数据,抽取数据。
loc函数是通过行标签索引行数据
iloc函数是通过行号索引行数据
ix函数是通过行标签或者行号索引行数据,简单来说就是loc和iloc的混合体
iloc主要使用数字来索引数据,而不能使用字符型的标签来索引数据。而loc则刚好相反,只能使用字符型标签来索引数据,
不能使用数字来索引数据,不过有特殊情况,当数据框dataframe的行标签或者列标签为数字,loc就可以来其来索引。
ix是一种混合索引,字符型标签和整型数据索引都可以
'''
import sys
import pandas as pd
import numpy as np
reload(sys)
sys.setdefaultencoding('utf-8')def testFunc():''''''data=np.arange(36).reshape(6,6)print 'data:'print datadf=pd.DataFrame(data)print 'dataFrame:'print dfprint '-*'*30print df.loc[1]print '-*'*30print df.iloc[1]print '-*'*30print df.loc[:,[0,4]]print '-*'*30print df.iloc[:,[0,4]]print '-*'*30print u"将数值型索引替换为字符型索引:"df.index=['ID','Number','Height','Weight','Circle','Space'] print df print '-*'*30try:print df.loc[0]except Exception,e:print 'Exception: ',eprint '-*'*30try:print df.iloc['Height'] except Exception,e:print 'Exception: ',eprint '-|=|'*30print df.iloc[0] print df.loc['Circle']print '-*'*30print u"将数值型列索引替换为字符型列索引:"df.columns=['zero','one','two','three','four','five']print dfprint '-*'*30print df.loc[:,'zero']print '-*'*30try:print df.iloc[:,'one'] except Exception,e:print 'Exception: ',eprint '-*'*30print u"ix抽取数据示例"print df.ix[5]print '-*'*30print df.ix[:,'three']print '-*'*30print df.ix[:,'one']print '-*'*30print df.ix[:,'five']print u"获取多行数据:"print df.loc['Height':'Space']print df.iloc[2:]print df.ix['Height':'Space']print df.ix[2:]print '*%'*40print u"获取多列数据:"print df.loc[:,'zero':'four']print df.iloc[:,0:5]print df.ix[:,'zero':'four']print df.ix[:,0:5]if __name__=='__main__':testFunc()

结果如下:

data:
[[ 0  1  2  3  4  5][ 6  7  8  9 10 11][12 13 14 15 16 17][18 19 20 21 22 23][24 25 26 27 28 29][30 31 32 33 34 35]]
dataFrame:0   1   2   3   4   5
0   0   1   2   3   4   5
1   6   7   8   9  10  11
2  12  13  14  15  16  17
3  18  19  20  21  22  23
4  24  25  26  27  28  29
5  30  31  32  33  34  35
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0     6
1     7
2     8
3     9
4    10
5    11
Name: 1, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
0     6
1     7
2     8
3     9
4    10
5    11
Name: 1, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*0   4
0   0   4
1   6  10
2  12  16
3  18  22
4  24  28
5  30  34
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*0   4
0   0   4
1   6  10
2  12  16
3  18  22
4  24  28
5  30  34
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
将数值型索引替换为字符型索引:0   1   2   3   4   5
ID       0   1   2   3   4   5
Number   6   7   8   9  10  11
Height  12  13  14  15  16  17
Weight  18  19  20  21  22  23
Circle  24  25  26  27  28  29
Space   30  31  32  33  34  35
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Exception:  cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <type 'int'>
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Exception:  cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [Height] of <type 'str'>
-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|
0    0
1    1
2    2
3    3
4    4
5    5
Name: ID, dtype: int32
0    24
1    25
2    26
3    27
4    28
5    29
Name: Circle, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
将数值型列索引替换为字符型列索引:zero  one  two  three  four  five
ID         0    1    2      3     4     5
Number     6    7    8      9    10    11
Height    12   13   14     15    16    17
Weight    18   19   20     21    22    23
Circle    24   25   26     27    28    29
Space     30   31   32     33    34    35
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ID         0
Number     6
Height    12
Weight    18
Circle    24
Space     30
Name: zero, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Exception:  Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ix抽取数据示例
zero     30
one      31
two      32
three    33
four     34
five     35
Name: Space, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ID         3
Number     9
Height    15
Weight    21
Circle    27
Space     33
Name: three, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ID         1
Number     7
Height    13
Weight    19
Circle    25
Space     31
Name: one, dtype: int32
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ID         5
Number    11
Height    17
Weight    23
Circle    29
Space     35
Name: five, dtype: int32
获取多行数据:zero  one  two  three  four  five
Height    12   13   14     15    16    17
Weight    18   19   20     21    22    23
Circle    24   25   26     27    28    29
Space     30   31   32     33    34    35zero  one  two  three  four  five
Height    12   13   14     15    16    17
Weight    18   19   20     21    22    23
Circle    24   25   26     27    28    29
Space     30   31   32     33    34    35zero  one  two  three  four  five
Height    12   13   14     15    16    17
Weight    18   19   20     21    22    23
Circle    24   25   26     27    28    29
Space     30   31   32     33    34    35zero  one  two  three  four  five
Height    12   13   14     15    16    17
Weight    18   19   20     21    22    23
Circle    24   25   26     27    28    29
Space     30   31   32     33    34    35
*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%
获取多列数据:zero  one  two  three  four
ID         0    1    2      3     4
Number     6    7    8      9    10
Height    12   13   14     15    16
Weight    18   19   20     21    22
Circle    24   25   26     27    28
Space     30   31   32     33    34zero  one  two  three  four
ID         0    1    2      3     4
Number     6    7    8      9    10
Height    12   13   14     15    16
Weight    18   19   20     21    22
Circle    24   25   26     27    28
Space     30   31   32     33    34zero  one  two  three  four
ID         0    1    2      3     4
Number     6    7    8      9    10
Height    12   13   14     15    16
Weight    18   19   20     21    22
Circle    24   25   26     27    28
Space     30   31   32     33    34zero  one  two  three  four
ID         0    1    2      3     4
Number     6    7    8      9    10
Height    12   13   14     15    16
Weight    18   19   20     21    22
Circle    24   25   26     27    28
Space     30   31   32     33    34

在实际的工作中,如果能够很熟练地掌握这些常用的数据操作函数对于效率的提升相信还是很有帮助的。

Pandas数据框索引函数 iloc、loc和ix学习使用相关推荐

  1. Pandas数据分析14——pandas数据框的多层索引

    参考书目:<深入浅出Pandas:利用Python进行数据处理与分析> pandas数据框针对高维数据,也有多层索引的办法去应对.多层数据一般长这个样子 可以看到AB两大列,下面又有xy两 ...

  2. pandas中的切片函数: .iloc()、.loc()、.ix() 的区别

    文章目录 pandas中的切片函数:`.iloc()`.`.loc()`.`.ix()` 1.首先创建一个DataFrame iloc:通过行/列号选取数据 1. .iloc就是通过行/列号来选取数据 ...

  3. pandas数据框loc属性语法及示例

    pandas.DataFrame.loc[] 是数据框的属性,通过指定标签或布尔数组来访问数据框的一组行和列.pandas.DataFrame是带有轴标签的二维表数据结构,如列和行.从数据框选择列结果 ...

  4. pandas数据框,统计某列或者某行数据元素的个数

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/sinat_38893241/artic ...

  5. 在pandas数据框中选择多个列

    本文翻译自:Selecting multiple columns in a pandas dataframe I have data in different columns but I don't ...

  6. xlsxwriter写行合并_使用xlsxwriter将pandas数据框写入excel并包含“write-rich”字符串格式...

    以下是可复制的,并产生所需的输出. import xlsxwriter, pandas as pd workbook = xlsxwriter.Workbook('pandas_with_rich_s ...

  7. python数据分析之pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]

    1 引言 Pandas是作为Python数据分析著名的工具包,提供了多种数据选取的方法,方便实用.本文主要介绍Pandas的几种数据选取的方法. Pandas中,数据主要保存为Dataframe和Se ...

  8. python中df是什么_python数据分析之pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]...

    1 引言 Pandas是作为Python数据分析著名的工具包,提供了多种数据选取的方法,方便实用.本文主要介绍Pandas的几种数据选取的方法. Pandas中,数据主要保存为Dataframe和Se ...

  9. Pandas 数据框增、删、改、查、去重、抽样基本操作

    总括 pandas的索引函数主要有三种: loc 标签索引,行和列的名称 iloc 整型索引(绝对位置索引),绝对意义上的几行几列,起始索引为0 ix 是 iloc 和 loc的合体 at是loc的快 ...

  10. python更改数据框指定位置的数据_python – 更改数据框中多个loc的最快方法

    我有一个包含100万行的pandas数据帧.我想用另一组值替换列中900,000行的值.有没有for循环(我需要两天才能完成)的快速方法吗? 例如,查看此示例数据框,其中我已将100万行压缩为8行 i ...

最新文章

  1. C++ 重载运算符简单举例
  2. 【最新】南京大学人工智能学院《模式识别》课程PPT
  3. [转]深入理解G1垃圾收集器
  4. geforce experience_Nvidia? Geforce? Experience?是什么?如何使用呢?
  5. MySQL 安装手册(5.7.20)
  6. 5.4.2 Using Hibernate templates
  7. Serializable 接口与 Java 序列化与反序列化
  8. 分享Monaco.ttf字体(Mac样式)
  9. epsonl360打印机连接电脑_epsonl360打印机脱机如何解决
  10. ZEROTIER ONE实现内网穿透
  11. 【Python】将多个jpg合并成一个pdf
  12. DataFrame使用pd.sample()随机选取N行数据
  13. 2022年智源社区年度热点推荐丨新春集锦
  14. layui实现导出全部数据Excel
  15. 如何设置WordPress文章特色图像(Featured Image)
  16. open、write、read函数总结(初学者,请见谅)
  17. nat123端口映射linux,nat123 linux版下载
  18. php制作留言板的题_利用PHP制作留言板实现代码
  19. python写我的世界启动器_的世界(Minecraft)如何自己制作启动器-百度经验
  20. 使用TPC-DS对kudu进行基准测试

热门文章

  1. dubbo源码分析12——服务暴露3_doExportUrls()方法分析
  2. 虚拟机是怎么实现的?(转)
  3. AI2(App Inventor 2)离线版服务器单机版
  4. 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)...
  5. 今天中午还收到了,一条诈骗短信,说是中奖了
  6. MyEclipse 10.5与ExtJS 4.1.1自动代码提示
  7. 关于bayes错误率计算公式P[error] = P[error | x]P(x)dx
  8. 教育局查询2021年高考成绩,关于广东省2021年普通高考英语听说考试成绩发布有关事宜的通知...
  9. matlab红字怎么删除,matlab-系统爱好者
  10. 拓端tecdat|R语言:逻辑回归ROC曲线对角线分析过程及结果