先看代码:

In [46]: import pandas as pd

In [47]: data = [[1,2,3],[4,5,6]]

In [48]: index = [0,1]

In [49]: columns=['a','b','c']

In [50]: df = pd.DataFrame(data=data, index=index, columns=columns)

In [51]: df
Out[51]:
a b c
0 1 2 3
1 4 5 6

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


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

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

In [53]: import pandas as pd
...: data = [[1,2,3],[4,5,6]]
...: index = ['d','e']
...: columns=['a','b','c']
...: df = pd.DataFrame(data=data, index=index, columns=columns)
...: In [54]: df
Out[54]:
a b c
d 1 2 3
e 4 5 6 In [55]: df.loc['d']
Out[55]:
a 1
b 2
c 3
Name: d, dtype: int64

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

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

1.4 loc可以获取多行数据


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

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

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

1.6 loc扩展——索引某列

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

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

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

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

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

先看之前df数据:

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

现在调用iloc命令

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

2.2 通过行标签索引会报错

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

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

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

2.4 iloc索引列数据

In [64]: df.iloc[:,[0]]
Out[64]:
a
d 1
e 4 In [65]: df.iloc[:,[1]]
Out[65]:
b
d 2
e 5

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

3.1 通过行号索引

先看之前df数据:

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

现在看看.ix用法

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

3.2 通过行标签索引

In [67]: df.ix['e']
Out[67]:
a 4
b 5
c 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. div块级元素获取焦点

    在做弹出层时需要对div获取失去焦点 focus blur只是针对form表单控件的,而对于 span , div , li 之类的,则没办法触发它们的动作 几个事件(摘自w3c). blur事件: ...

  2. web安全漏洞防护

    Password type input with autocomplete enabled The autocomplete attribute works with the following &l ...

  3. Java-iBATIS

    1.是什么? 2010年迁移到了google code,并且改名为MyBatis.ibatis1.ibatis2.到了版本3就改名为mybatis. iBATIS的是一个持久层框架,它能够自动在 Ja ...

  4. javaScript Number对象

    Number 对象 Number 对象是原始数值的包装对象. 创建 Number 对象的语法: var myNum=new Number(value); var myNum=Number(value) ...

  5. 安装git和配置

     首先更新系统 yum  -y update  安装依赖的包 yum -y install curl-devel expat-devel gettext-devel openssl-devel zli ...

  6. ssm框架整合-过程总结(第三次周总结)

    本周主要是完成前端界面和后端的整合. 犹豫前后端的工作完成程度不一致,只实现了部分整合. 登录界面. 可能自己最近没有把重心放在短学期的项目上,导致我们工作的总体进度都要比别慢. 虽然我们只是三个人的 ...

  7. 【转】 HMC与VIOS对新LPAR提供存储与网络虚拟化的支持

    前面的几篇博文的操作环境都是在IVM下,IVM可以看作是VIOS的一部分,或者是对VIOS功能的一个扩展,一个IVM只能管理1台物理服务器,而HMC则是一对多.在有HMC来管理物理服务器的情形下,VI ...

  8. log日志框架和LocationAwareLogger问题

    遇到了同样的问题, 我的解决办法是在pom.xml中增加如下配置,去除对于jcl-over-slf4j.jar的依赖. <exclusions>                <ex ...

  9. Qt 如何像 VS 一样创建项目模版?

    qt 存储模版路径位置:Qt\Qt5.9.5\Tools\QtCreator\share\qtcreator\templates\wizards 在里面随意复制一个模版,修改三项即可在 qt 中显示该 ...

  10. Linux用户相关文件之密码文件

    1.文件地址: /etc/shadow ----------. 1 root root 842 10月 6 13:09 /etc/shadow 2.文件内容: xiaol_1:$6$NdCAnK3y$ ...