zoukankan      html  css  js  c++  java
  • 学习笔记6—pandas中ix,loc,iloc有什么区别?

    直接看例子:

    >>> data = pd.Series(np.arange(10), index=[49,48,47,46,45, 1, 2, 3, 4, 5])
    >>> data
    49    0
    48    1
    47    2
    46    3
    45    4
    1     5
    2     6
    3     7
    4     8
    5     9
    dtype: int64
    >>> data.iloc[:3]
    49    0
    48    1
    47    2
    dtype: int64
    >>> data.loc[:3]
    49    0
    48    1
    47    2
    46    3
    45    4
    1     5
    2     6
    3     7
    dtype: int64
    >>> data.ix[:3]
    49    0
    48    1
    47    2
    46    3
    45    4
    1     5
    2     6
    3     7
    dtype: int64
    
    • 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
    • 36
    • 37
    • 38
    • 39

    解析:
    loc 在index的标签上进行索引,范围包括start和end.
    iloc 在index的位置上进行索引,不包括end.
    ix 先在index的标签上索引,索引不到就在index的位置上索引(如果index非全整数),不包括end.

    >>> data.iloc[:6]
    49    0
    48    1
    47    2
    46    3
    45    4
    1     5
    dtype: int64
    >>> data.loc[:6]
    KeyError: 6
    >>> data.ix[:6] #因为index里面不包含标签6,index都是整数
    KeyError: 6
    >>> data= pd.Series(np.arange(10), index=['a','b','c','d','e', 1, 2, 3, 4, 5])
    >>> data
    a    0
    b    1
    c    2
    d    3
    e    4
    1    5
    2    6
    3    7
    4    8
    5    9
    dtype: int64
    >>> data.ix[:6]
    a    0
    b    1
    c    2
    d    3
    e    4
    1    5
    dtype: int64
    >>> data.loc[:6]
    TypeError: cannot do slice indexing
  • 相关阅读:
    python与数据库(SQLServer、MySQL、Redis、MongoDB)
    网络表示学习 图神经网络资料整理
    Java学习笔记 算法 Algorithms Fourth Edition
    word2vec 代码解读
    爬虫python的世界
    mac 字幕生成工具
    CS224W Analysis of Networks
    SNAP macos
    python基础代码
    Keras笔记
  • 原文地址:https://www.cnblogs.com/hechangchun/p/9557860.html
Copyright © 2011-2022 走看看