我有以下代码,使用Pandas将股票交易订单列表放入数据框中,我需要按值对数据框进行排序,但不能理解为什么会出现键错误。
代码如下:
for row in values:
if row[1] == '#N/A' or row[2] == '#N/A' or row[3] == '#N/A':
continue
symbol = row[0]
price = float(row[3])
open_p = float(row[1])
previous_close = float(row[2])
volume = float(row[4])
change_at_open = round((open_p - previous_close)/previous_close,4)
change_since_open = round((price - open_p)/open_p,4)
if change_at_open > min_change_at_open and change_since_open < -revert_level and price > 1 and volume > 50000:
quantity = math.floor(ptf_value/num_pos/price)
#print('%s, %s, %s, %s, %s' % (symbol, price, change_at_open, change_since_open, quantity))
signal_count += 1
orders[signal_count] = {'symbol':symbol,'price':price,'quantity':quantity, 'change_at_open':change_at_open}
df = pd.DataFrame(data = orders)
df = df.T
df.nlargest(10,['change_at_open'])
数据帧df的触点如下:
change_at_open price quantity symbol
1 0.1634 1.55 645 IZEA
2 0.1867 64 15 BJRI
3 0.1101 10.6 94 DFRG
4 0.0741 13.6 73 DGII
5 0.087 23.2 43 EHTH
6 0.1889 2.2 454 HSGX
7 0.0652 17.6 56 CHRS
8 0.1054 3.74 267 MEIP
9 0.0758 44 22 NATI
10 0.0812 1.86 537 OBLN
11 0.0763 1.11 900 ORPN
12 0.0956 6.06 165 RMBL
13 0.1662 73.8 13 TEAM
14 0.0789 2.85 350 TTPH
15 0.1185 1.3 769 VTVT
因此,列名看起来非常简单。我尝试对df进行排序或获取10个大对象'change_at_open‘,但我总是得到以下错误:
Traceback (most recent call last):
File "<ipython-input-133-6a99d27bb6bb>", line 157, in <module>
df.nlargest(10,['change_at_open'])
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 4625, in nlargest
columns=columns).nlargest()
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/algorithms.py", line 1081, in nlargest
return self.compute('nlargest')
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/algorithms.py", line 1185, in compute
dtype = frame[column].dtype
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2685, in __getitem__
return self._getitem_column(key)
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2692, in _getitem_column
return self._get_item_cache(key)
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 2486, in _get_item_cache
values = self._data.get(item)
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "/Users/gilles/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3065, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'change_at_open'
我如何调试它呢?
发布于 2018-07-29 04:52:13
我找到了问题的原因,但我在问题中粘贴的代码中找不到原因。这只是一个缩进问题(我是python的新手,我整天都在看代码,但没有看到这一点!),dataframe的创建应该是在for循环完成之后才创建的,这就是导致问题的原因。
发布于 2018-07-29 04:54:58
只有当密钥不可用时,它才会出现。
因此,要解决这个问题,您可以为每个键设置一个If条件,只检查该键是否存在,或者可以使用KeyError异常通过异常处理来处理它
if:
# code block
except KeyError, e:
pass
https://stackoverflow.com/questions/51575116
复制相似问题