前几天写分析方法,遇到的一个错误:

具体我已经在stackoverflow 里面得到了详细的解答,下面我把问题和解决办法总结一下,方便日后的回顾

问题:

sql = "select {}, {} from {} where {};".format(v1, v2, table, where)
df = pd.read_sql(sql, app.config.get('sqlalchemy_engine'))
df_dropna = df.dropna()
dddd = df_dropna[v2]
print type(dddd)
print dddd[1]
# print dddd # Have the answer
print dddd[-1] # keyerror: -1

错误:

File "/usr/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/code/my_code/app4wp/app/views.py", line 136, in varianc_get
print dddd[-1]
File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 601, in __getitem__
result = self.index.get_value(self, key)
File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 2169, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:3567) File "pandas/index.pyx", line 113, in pandas.index.IndexEngine.get_value (pandas/index.c:3250) File "pandas/index.pyx", line 161, in pandas.index.IndexEngine.get_loc (pandas/index.c:4289) File "pandas/src/hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8555) File "pandas/src/hashtable_class_helper.pxi", line 410, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8499) KeyError: -1

为什么呢?

When you use dddd[-1] it is looking for a column with the -1 key, that does not exist. So, to do index slicing you must use .iloc.
Pandas dataframes work differently from regular python lists,d ictionaries and etc....

哈哈,就这么解决了!!!

print dddd.iloc[-1]

pandas Series KeyError: -1的更多相关文章

  1. pandas Series的sort_values()方法

    pandas Series的 sort_values() 方法能对Series进行排序,返回一个新的Series: s = pd.Series([np.nan, 1, 3, 10, 5]) 升序排列: ...

  2. pandas.Series

    1.系列(Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组.轴标签统称为索引. Pandas系列可以使用以下构造函数创建 - pandas.Series ...

  3. pandas数组(pandas Series)-(5)apply方法自定义函数

    有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...

  4. pandas数组(pandas Series)-(4)NaN的处理

    上一篇pandas数组(pandas Series)-(3)向量化运算里说到,将两个 pandas Series 进行向量化运算的时候,如果某个 key 索引只在其中一个 Series 里出现,计算的 ...

  5. pandas数组(pandas Series)-(3)向量化运算

    这篇介绍下有index索引的pandas Series是如何进行向量化运算的: 1. index索引数组相同: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b' ...

  6. pandas数组(pandas Series)-(2)

    pandas Series 比 numpy array 要强大很多,体现在很多方面 首先, pandas Series 有一些方法,比如: describe 方法可以给出 Series 的一些分析数据 ...

  7. python. pandas(series,dataframe,index) method test

    python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...

  8. Python Pandas -- Series

    pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...

  9. pandas.Series.value_counts

    pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...

随机推荐

  1. 进程process与线程thread

    进程:process是一个外理过程,即然是外理过程,那么它就有生命周期,从进程的启动,运行,直到运行结束,进程终止.进程是程序的执行实例,即运行中的程序,同时也是程序的一个副本,程序是放置于磁盘的,而 ...

  2. 关于https中的算法

    1,对称加密算法,是指加密和解密使用相同的密钥,典型的算法有RSA,DSA,DH 2,非对称加密算法:又称为公钥加密算法,是指加密和解密使用不同的密钥,公共的公钥用于加密,私钥用于解密,比如第一次请求 ...

  3. ossec变更alert等级及配置邮件预警

    一.场景 当攻击者尝试使用字典对某一台主机的sshd服务进行暴力破解的时候,如果我们能第一时间受到攻击预警的邮件的话,对安全人员或者运维人员来说都能做出快速响应.而使用ossec恰巧可以完成这一工作, ...

  4. Splunk Web页面的登录密码忘记了怎么办

    splunk的web登录密码忘记的话,可以使用以下方法重置. 一.关闭splunk服务 /opt/splunk/bin/splunk stop 二.删除默认密码配置文件 三.重启启动splunk服务, ...

  5. VS注释与取消注释快捷键

    最近在使用VS2010开发ASP.Net,突然发现想全部注释时找不到注释的快捷键,网上查了下,原来很简单,只是需要使用组合键. 注释:        先CTRL+K,然后CTRL+C 取消注释: 先C ...

  6. NGUI制作字体的三种方法

    主要参考两篇博文: (1).NGUI制作字体的三种方法 (2).使用位图字体工具BMFont从图片生成自定义字体 1.BMFont下载地址 http://www.angelcode.com/produ ...

  7. 【嵌入式】FS2410非操作系统外围资源测试

    在刚接触FS2410时,其实这个测试也没有多大意义,但是对于以后来说,当一个产品做成功时,产品测试还是一个必须经过的一个阶段,所以这个流程还是有必要走一下! 在非操作系统下,主要进行RTC测试,按键测 ...

  8. ubuntu 解压命令

    ubuntu解压命令.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————. ...

  9. Mybatis的mapper注册

    既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了.但是首先我们需要告诉 MyBatis 到哪里去找到这些语句. Java 在自动查找这方面没有提供一个很好的方法 ...

  10. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...