使用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. TCP/IP详解读书笔记:链路层

    在TCP/IP协议族中,链路层主要有三个目的: 1)为IP模块发送和接受IP数据报: 2)为ARP模块发送ARP请求和接受ARP应答: 3)为RARP模块发送RARP请求和接受RARP应答: 以太网和 ...

  2. 设置Tomcat编码(UTF-8)

    Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决. 在tomcat的conf目录下,编辑server.xml配置文件,在Conne ...

  3. git三种模式及常用命令

    Git的三种重要模式,分别是已提交.已修改.已暂存. 已提交(committed):表示数据文件已经顺利提交到Git数据库中. 已修改(modified):表示数据文件已经被修改,但未被保存到Git数 ...

  4. java模拟从http上下载文件

    1.依赖 Apache httpclient 包. 2.代码 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = ...

  5. linux web.py spawn-fcgi web.py 配置

    本来要用uwsgi,但是...介于以前说过...这台服务器略老...redhat 3的系统...确实很老,没法用yum,没法安装很多东西,打算自己编译uwsgi,但是编译各种错误...花了快一天,最后 ...

  6. Windows的静态库使用步骤

    windows库程序: 1.静态库程序 - 运行时不独立存在,会被链接到可执行文件或者动态库中,目标程序的归档. 文件扩展名:LIB 2.动态库程序 - 运行时独立存在,不会被链接到可执行文件或其他动 ...

  7. 嵌入式开发之信号采集同步---VSYNC和HSYNC的作用以及它们两者之间的关系

    VSYNC和HSYNC的作用以及它们两者之间的关系 VSYNC和HSYNC的作用以及它们两者之间的关系 VSYNC和HSYNC是什么 VSYNC: vertical synchronization,指 ...

  8. python 之 多进程

    阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiprocessingpython中的多线程 ...

  9. java was started but returned exit code=1

    将eclipse.ini文件删掉, 重启eclipse 会自动生成一个eclipse.ini就可以了~

  10. dns解决测试微信二级域名访问问题

    背景介绍: 1:解决本地不能通过域名访问问题: 2:解决微信设置二级域名且本地iis站点使用非80端口号问题: ps:网站中微信部分在global中设置了重定向,代码已经修改为必须通过“wechat. ...