美文网首页
Python Study notebook1---compare

Python Study notebook1---compare

作者: 佳馥Jasmin | 来源:发表于2017-02-14 20:22 被阅读0次

    The explains for both are:

    DataFrame.loc:Purely label-location based indexer for selection by label.

    .loc[] is primarily label based, but may also be used with a boolean array.

    DataFrame.iloc:Purely integer-location based indexing for selection by position.

    .iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

    
    import pandas as pd
    
    data = [[1,2,3],[4,5,6]]
    
    index = ['a','b']
    
    columns = ['c','d','e']
    
    df = pd.DataFrame(data=data, index=index, columns=columns)
    
    '''
    
        c  d  e
    
    a  1  2  3
    
    b  4  5  6
    
    '''
    
    print df.loc['a']
    
    print df.iloc[1]
    
    
    
    

    The output:
    c 1
    d 2
    e 3
    Name: a, dtype: int64
    c 4
    d 5
    e 6
    Name: b, dtype: int64

    The parameter i of pa.loc[i] is primarily label of row,the parameter i of pa.iloc[i] is integer positio of row

    1. How to use pa.loc

    1.1 We can also use pa.loc to get several rows data:

    print df.loc['a']
    

    The output is:
    c d e
    a 1 2 3
    b 4 5 6

    1.2 Index to raw'a' column 'd'&'e'

    print df.loc['a',['d','e']] 
    

    The output is:
    d 2
    e 3
    Name: a, dtype: int64

    1.3 Index by column

    print df.loc[:,'c']
    

    The output is:
    a 1
    b 4
    Name: c, dtype: int64

    2. How to use pa.iloc

    2.1 We can also use pa.iloc to get several rows data:

    print df.iloc[0:] 
    

    The output is:
    c d e
    a 1 2 3
    b 4 5 6
    Name: b, dtype: int64

    2.2 Index to first raw second column

    print df.iloc[0,[1]] 
    c```
    The output is:
    d    2
    Name: a, dtype: int64
    #### 1.3 Index by column
    

    print df.iloc[:,[1]]

    
    The output is:
       d
    a  2
    b  5

    相关文章

      网友评论

          本文标题:Python Study notebook1---compare

          本文链接:https://www.haomeiwen.com/subject/oukfwttx.html