先看代码:

  1. In [46]: import pandas as pd
  2. In [47]: data = [[1,2,3],[4,5,6]]
  3. In [48]: index = [0,1]
  4. In [49]: columns=['a','b','c']
  5. In [50]: df = pd.DataFrame(data=data, index=index, columns=columns)
  6. In [51]: df
  7. Out[51]:
  8. a b c
  9. 0 1 2 3
  10. 1 4 5 6

1. loc——通过行标签索引行数据


  1. In [52]: df.loc[1]
  2. Out[52]:
  3. a 4
  4. b 5
  5. c 6
  6. Name: 1, dtype: int64

1.2 loc['d']表示索引的是第’d’行(index 是字符)

  1. In [53]: import pandas as pd
  2. ...: data = [[1,2,3],[4,5,6]]
  3. ...: index = ['d','e']
  4. ...: columns=['a','b','c']
  5. ...: df = pd.DataFrame(data=data, index=index, columns=columns)
  6. ...:
  7. In [54]: df
  8. Out[54]:
  9. a b c
  10. d 1 2 3
  11. e 4 5 6
  12. In [55]: df.loc['d']
  13. Out[55]:
  14. a 1
  15. b 2
  16. c 3
  17. Name: d, dtype: int64

1.3 如果想索引列数据,像这样做会报错

  1. In [56]: df.loc['a']
  2. Traceback (most recent call last):
  3. File "<ipython-input-56-5dbae926782f>", line 1, in <module>
  4. df.loc['a']
  5. File "E:\Anaconda\lib\site-packages\pandas\core\indexing.py", line 1328, in __getitem__
  6. return self._getitem_axis(key, axis=0)
  7. ...
  8. KeyError: 'the label [a] is not in the [index]'

1.4 loc可以获取多行数据


  1. In [57]: df.loc['d':]
  2. Out[57]:
  3. a b c
  4. d 1 2 3
  5. e 4 5 6

1.5 loc扩展——索引某行某列

  1. In [58]: df.loc['d',['b','c']]
  2. Out[58]:
  3. b 2
  4. c 3
  5. Name: d, dtype: int64

1.6 loc扩展——索引某列

  1. In [59]: df.loc[:,['c']]
  2. Out[59]:
  3. c
  4. d 3
  5. e 6

当然获取某列数据最直接的方式是df.[列标签],但是当列标签未知时可以通过这种方式获取列数据。

需要注意的是,dataframe的索引[1:3]是包含1,2,3的,与平时的不同。

2. iloc——通过行号获取行数据

2.1 想要获取哪一行就输入该行数字

先看之前df数据:

  1. In [54]: df
  2. Out[54]:
  3. a b c
  4. d 1 2 3
  5. e 4 5 6

现在调用iloc命令

  1. In [60]: df.iloc[1] #获取第1行
  2. Out[60]:
  3. a 4
  4. b 5
  5. c 6
  6. Name: e, dtype: int64
  7. In [61]: df.iloc[0]  #获取第0行
  8. Out[61]:
  9. a 1
  10. b 2
  11. c 3
  12. Name: d, dtype: int64

2.2 通过行标签索引会报错

  1. In [62]: df.iloc['a']
  2. Traceback (most recent call last):
  3. File "<ipython-input-62-0c5fe4e92254>", line 1, in <module>
  4. df.iloc['a']
  5. File "E:\Anaconda\lib\site-packages\pandas\core\indexing.py", line 1328, in __getitem__
  6. return self._getitem_axis(key, axis=0)
  7. ...
  8. TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [a] of <class 'str'>

2.3 同样通过行号可以索引多行

  1. In [63]: df.iloc[0:]   #获取0和其他行
  2. Out[63]:
  3. a b c
  4. d 1 2 3
  5. e 4 5 6

2.4 iloc索引列数据

  1. In [64]: df.iloc[:,[0]]
  2. Out[64]:
  3. a
  4. d 1
  5. e 4
  6. In [65]: df.iloc[:,[1]]
  7. Out[65]:
  8. b
  9. d 2
  10. e 5

3. ix——结合前两种的混合索引 (现在ix用法不推荐,这是Python2.x常用的)

3.1 通过行号索引

先看之前df数据:

  1. In [54]: df
  2. Out[54]:
  3. a b c
  4. d 1 2 3
  5. e 4 5 6

现在看看.ix用法

  1. In [66]: df.ix[1]
  2. __main__:1: DeprecationWarning: .ix is deprecated. Please use .loc for label based indexing or .iloc for positional indexing
  3. See the documentation here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  4. Out[66]:
  5. a 4
  6. b 5
  7. c 6
  8. Name: e, dtype: int64

3.2 通过行标签索引

  1. In [67]: df.ix['e']
  2. Out[67]:
  3. a 4
  4. b 5
  5. c 6
  6. Name: e, dtype: int64

参考来源:https://blog.csdn.net/roamer314/article/details/52179191

Pandas的 loc iloc ix 区别的更多相关文章

  1. Pandas:loc iloc ix用法

    参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...

  2. python pandas 中 loc & iloc 用法区别

    转自:https://blog.csdn.net/qq_21840201/article/details/80725433 ### 随机生DataFrame 类型数据import pandas as ...

  3. pandas 定位 loc,iloc,ix

    In [114]: df Out[114]: A B C D 2018-06-30 0.318501 0.613145 0.485612 0.918663 2018-07-31 0.614796 0. ...

  4. pandas的loc, iloc, ix的操作

    参考: https://blog.csdn.net/xw_classmate/article/details/51333646 1. loc——通过行标签索引行数据 2. iloc——通过行号获取行数 ...

  5. Pandas之loc\iloc\ix

    ---------------------------------------------------------------------------------------------------- ...

  6. pandas中DataFrame的ix,loc,iloc索引方式的异同

    pandas中DataFrame的ix,loc,iloc索引方式的异同 1.loc: 按照标签索引,范围包括start和end 2.iloc: 在位置上进行索引,不包括end 3.ix: 先在inde ...

  7. [译]pandas中的iloc loc的区别?

    loc 从特定的 gets rows (or columns) with particular labels from the index. iloc gets rows (or columns) a ...

  8. 3、pandas的loc和iloc数据筛选

    选择列: 选择一列: 选择多列(选择的内容变成list,也就是要两个方括号): 选择一行或多行(loc函数): 选择连续的行(以索引标签为选择参数): 选择非连续的行(以索引标签为选择参数): 选择包 ...

  9. pandas 选取数据 修改数据 loc iloc []

    pandas选取数据可以通过 loc iloc  [] 来选取 使用loc选取某几列: user_fans_df = sample_data.loc[:,['uid','fans_count']] 使 ...

随机推荐

  1. azure绑定ssl,godaddy的ssl证书

    域名是godaddy 申请的,证书也是godaddy 购买的,DV证书. godaddy购买证书后,申请ssl,需要输入,csr.网上找的csr生成工具,我使用  https://myssl.com/ ...

  2. GoogleMap-------manifest文件配置

    前言:在使用GoopleMap之前需要配置manifest文件 1.这个可有可无,com.xhm.meishi是项目的包名 <!-- 声明调用这个应用需要的权限 --> <permi ...

  3. c/c++输入处理,制定变量参数和值

    void usage(char* s){ fprintf(stderr, "\n"); fprintf(stderr, "%s -s <source file> ...

  4. HDU2844 Coins 多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. PHP中常用的输出语句比较?

    面试中经常问到这个,可以看下. 面试问题:比较echo print() print_r()  var_dump()? echo(): 可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(la ...

  6. HTML5新增的语义标签和IE版本低的兼容性问题

    <!DOCTYPE html><html> <head> <!-- HTML5中浏览器兼容(较低版本的IE浏览器不支持H5的布局):需要在<head&g ...

  7. 安装mysql报错—解决方法:error while loading shared libraries: libssl.so.6

    for 32bit ln -sf /usr/lib/libssl.so.10 /usr/lib/libssl.so.6ln -sf /usr/lib/libcrypto.so.10 /usr/lib/ ...

  8. js特殊字符过滤

    //匹配中文 数字 字母 下划线 var checkInput = function (str) { var pattern =var pattern = /^[\w\u4e00-\u9fa5]+$/ ...

  9. JSTL 标签库

    1. JSTL 概述 JSTL 是 apache 对 EL 表达式的扩展, JSTL 是标签语言! 需要导入 jstl-1.2.jar 包 2. JSTL 标签库 core: 核心标签库; fmt: ...

  10. Python获取主机名

    import socket print socket.gethostname()