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

    在数据科学领域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 data
    df=pd.DataFrame(data)
    print 'dataFrame:'
    print df
    print '-*'*30
    print df.loc[1]
    print '-*'*30
    print df.iloc[1]
    print '-*'*30
    print df.loc[:,[0,4]]
    print '-*'*30
    print df.iloc[:,[0,4]]
    print '-*'*30

    print u"将数值型索引替换为字符型索引:"
    df.index=['ID','Number','Height','Weight','Circle','Space'] 
    print df 
    print '-*'*30
    try:
        print df.loc[0]
    except Exception,e:
        print 'Exception: ',e
    print '-*'*30
    try:
        print df.iloc['Height'] 
    except Exception,e:
        print 'Exception: ',e
    print '-|=|'*30
    print df.iloc[0] 
    print df.loc['Circle']
    print '-*'*30
    
    print u"将数值型列索引替换为字符型列索引:"
    df.columns=['zero','one','two','three','four','five']
    print df
    print '-*'*30
    print df.loc[:,'zero']
    print '-*'*30

    try:
        print df.iloc[:,'one'] 
    except Exception,e:
        print 'Exception: ',e
    print '-*'*30

    print u"ix抽取数据示例"
    print df.ix[5]
    print '-*'*30
    print df.ix[:,'three']
    print '-*'*30
    print df.ix[:,'one']
    print '-*'*30
    print df.ix[:,'five']

    print u"获取多行数据:"
    print df.loc['Height':'Space']
    print df.iloc[2:]
    print df.ix['Height':'Space']
    print df.ix[2:]

    print '*%'*40

    print 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    35
        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    35
        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    35
        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    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    34
        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    34
        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    34
        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    34

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

猜你喜欢

转载自blog.csdn.net/Together_CZ/article/details/84823138