使用pandas创建一个对象

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: df = pd.DataFrame(np.random.randn(6,4),index=pd.date_range('',periods=6),columns=list('ABCD'))

In [4]: df
Out[4]:
A B C D
2018-01-01 -0.603510 0.269480 0.197354 -0.433003
2018-01-02 1.230502 0.474616 1.473517 -0.627363
2018-01-03 -0.402034 0.569097 0.675872 -0.317995
2018-01-04 0.220638 0.527543 -1.140620 -0.348089
2018-01-05 -2.494331 0.593269 0.596578 1.653347
2018-01-06 -2.766239 -0.919777 0.462890 0.156048

如果你想得到第三行的数据:

如果你沿袭之前python切片的习惯,想直接取,那么需要改变一下方式。

KeyError                                  Traceback (most recent call last)
D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3062 try:
-> 3063 return self._engine.get_loc(key)
3064 except KeyError: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 2 During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last)
<ipython-input-5-b5f2749c85df> in <module>()
----> 1 df[2] D:\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2683 return self._getitem_multilevel(key)
2684 else:
-> 2685 return self._getitem_column(key)
2686
2687 def _getitem_column(self, key): D:\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
2690 # get column
2691 if self.columns.is_unique:
-> 2692 return self._get_item_cache(key)
2693
2694 # duplicate columns & possible reduce dimensionality D:\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
2484 res = cache.get(item)
2485 if res is None:
-> 2486 values = self._data.get(item)
2487 res = self._box_item_values(item, values)
2488 cache[item] = res D:\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
4113
4114 if not isna(item):
-> 4115 loc = self.items.get_loc(item)
4116 else:
4117 indexer = np.arange(len(self.items))[isna(self.items)] D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3063 return self._engine.get_loc(key)
3064 except KeyError:
-> 3065 return self._engine.get_loc(self._maybe_cast_indexer(key))
3066
3067 indexer = self.get_indexer([key], method=method, tolerance=tolerance) pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 2

df[2]存在语法错误

正确的做法其实有好多种:

方法1:

In [6]: df[2:3]
Out[6]:
A B C D
2018-01-03 -0.402034 0.569097 0.675872 -0.317995

方法2:

vIn [7]: df['':'']  #这里必须使用这种方式,不然会有语法错误
Out[7]:
A B C D
2018-01-03 -0.402034 0.569097 0.675872 -0.317995

刚才使用类似python单个切片的方式貌似不行,所以就要说到今天的重点,loc、iloc、ix

(1).loc:按照标签进行取值

In [8]: df.loc['2018/01/03']
Out[8]:
A -0.402034
B 0.569097
C 0.675872
D -0.317995
Name: 2018-01-03 00:00:00, dtype: float64

(2).iloc:按照标签进行取值

In [9]: df.iloc[2]
Out[9]:
A -0.402034
B 0.569097
C 0.675872
D -0.317995
Name: 2018-01-03 00:00:00, dtype: float64

(3)ix:混合缩影

In [10]: df.ix['2018/01/03']
Out[10]:
A -0.402034
B 0.569097
C 0.675872
D -0.317995
Name: 2018-01-03 00:00:00, dtype: float64 In [11]: df.ix[2]
Out[11]:
A -0.402034
B 0.569097
C 0.675872
D -0.317995
Name: 2018-01-03 00:00:00, dtype: float64

loc、iloc、ix比较的更多相关文章

  1. Pandas:loc iloc ix用法

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

  2. 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. ...

  3. Pandas的 loc iloc ix 区别

    先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: ...

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

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

  5. Pandas之loc\iloc\ix

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

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

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

  7. loc() iloc() at() iat()函数

    1 四个函数都是用于dataframe的定位 []用于直接定位. loc()函数是用真实索引,iloc()函数是用索引序号. loc()函数切片是左闭右闭,iloc()函数切片是左闭右开. at(), ...

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

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

  9. pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)

    数据介绍 先随机生成一组数据: import pandas as pd import numpy as np state = ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'N ...

  10. 学习笔记6—pandas中ix,loc,iloc有什么区别?

    直接看例子: >>> data = pd.Series(np.arange(10), index=[49,48,47,46,45, 1, 2, 3, 4, 5]) >>& ...

随机推荐

  1. ARP-Address Resolution Protocol-地址解析协议

    主要内容摘自:图解TCP/IP ARP是一种解决地址问题的协议.以目标IP地址为线索,用来定位下一个应该接受数据分包的网络设备的mac地址. 如果目标主机不在同一个链路上时,可以通过ARP查找下一跳路 ...

  2. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...

  3. spring+mybatis+javafx

    @Service用于标注业务层组件 @Controller用于标注控制层组件(如struts中的action) @Repository用于标注数据访问组件,即DAO组件. @Component泛指组件 ...

  4. 支付宝API接口开发相关文档以及实例

    支付宝实物即时到账接口,在下载该接口之前,请确定你已经签约了即时到账的服务协议,并且账号已经开通了即时到账的权限. 口常见错误问题搜集:http://union.alipay.com/alipay/z ...

  5. 【vijos】1763 Wormhole(贪心)

    https://vijos.org/p/1764 首先第一个虫洞一定是建在1号点. 证明如下: 假设一个虫洞在a,一个在b,a<b,那么走到k点的最短距离为 min{|x1-xk|, |x1-x ...

  6. 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...

  7. 总结 一下UML 类图的关系

    1,实线三角 表示 泛化  是一种继承关系,它指定了子类如何特化父类的所有特征和行为 2,虚线三角 表示 实现  是一种类与接口的关系,表示类是接口所有特征和行为的实现 3,空心菱形 表示 聚合 是整 ...

  8. elasticsearch.net search入门使用指南中文版

    原文:http://edu.dmeiyang.com/book/nestusing.html elasticsearch.net为什么会有两个客户端? Elasticsearch.Net是一个非常底层 ...

  9. iOS-iOS9.Plist插入网络安全xml

    //iOS9 设置网络 Plist文件 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsAr ...

  10. 《转》适用于开发人员的10个最佳ASP.NET的CMS系统

    1) mojoportal mojoPortal 是一个开源的.用 C# 编写的站点框架和内容管理系统,可以运行在 Windows 中的 ASP.NET 和 Linux/Mac OS X 中的 Mon ...