码迷,mamicode.com
首页 > 其他好文 > 详细

pandas Series KeyError: -1

时间:2017-06-27 11:56:28      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:xen   sel   app   错误   ase   var   with   from   error:   

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

具体我已经在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
View Code

 

为什么呢?

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

标签:xen   sel   app   错误   ase   var   with   from   error:   

原文地址:http://www.cnblogs.com/renfanzi/p/7084344.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!