pandas Series KeyError: -1
前几天写分析方法,遇到的一个错误:
具体我已经在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的更多相关文章
- pandas Series的sort_values()方法
pandas Series的 sort_values() 方法能对Series进行排序,返回一个新的Series: s = pd.Series([np.nan, 1, 3, 10, 5]) 升序排列: ...
- pandas.Series
1.系列(Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组.轴标签统称为索引. Pandas系列可以使用以下构造函数创建 - pandas.Series ...
- pandas数组(pandas Series)-(5)apply方法自定义函数
有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...
- pandas数组(pandas Series)-(4)NaN的处理
上一篇pandas数组(pandas Series)-(3)向量化运算里说到,将两个 pandas Series 进行向量化运算的时候,如果某个 key 索引只在其中一个 Series 里出现,计算的 ...
- pandas数组(pandas Series)-(3)向量化运算
这篇介绍下有index索引的pandas Series是如何进行向量化运算的: 1. index索引数组相同: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b' ...
- pandas数组(pandas Series)-(2)
pandas Series 比 numpy array 要强大很多,体现在很多方面 首先, pandas Series 有一些方法,比如: describe 方法可以给出 Series 的一些分析数据 ...
- python. pandas(series,dataframe,index) method test
python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...
- Python Pandas -- Series
pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...
- pandas.Series.value_counts
pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...
随机推荐
- listview 两个Item可以同时点击
android:splitMotionEvents="false" ListView的这个属性可以限制它不能同时点击两个Item
- java 字符串格式化
转:http://blog.sina.com.cn/s/blog_af26e3330101988v.html 1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化 ...
- elastic search internal
Realtime Search with Lucene http://2010.berlinbuzzwords.de/sites/2010.berlinbuzzwords.de/files/busch ...
- artificial%20intelligence%20a%20modern%20approach
http://stpk.cs.rtu.lv/sites/all/files/stpk/materiali/mi/artificial%20intelligence%20a%20modern%20app ...
- [AWS vs Azure] 云计算里AWS和Azure的探究(1)
转自:http://www.cnblogs.com/hotcan/archive/2013/01/31/2886794.html 云计算里AWS和Azure的探究(1) 全球领先的云的计算平台主要有两 ...
- cat正常,cat重定向到文件可能是乱码;解决办法
文件cat正常,cat重定向到文件可能是乱码:解决办法如下 .vimrc文件中增加如下两行 set fileencoding=utf8 set fileencodings=utf8 据一位老神医说,文 ...
- python 字符串编码 str和unicode 区别以及相互转化 decode('utf-8') encode('utf-8')
- 第一个驱动之字符设备驱动(二)mdev
mdev是busybox提供的一个工具,用在嵌入式系统中,相当于简化版的udev,作用是在系统启动和热插拔或动态加载驱动程序时, 自动创建设备节点.文件系统中的/dev目录下的设备节点都是由mdev创 ...
- mybatis中mysql和oracle的差异
1.applicationContext.xml中的配置差异: 在applicationContext.xml的数据源dataSource的配置中,mysql数据库需要心跳包的配置,而oracle中不 ...
- Delphi实现RGB色环的代码绘制(XE10.2+WIN764)
相关资料: http://blog.csdn.net/tokimemo/article/details/18702689 http://www.myexception.cn/delphi/215402 ...