Python Pandas DataFrame产生键盘错误

时间:2018-07-28 20:41:42

标签: python pandas

我有以下代码,使用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个bigt'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'

我该如何调试呢?

2 个答案:

答案 0 :(得分:0)

我从粘贴在问题中的代码中发现了问题的原因。这只是一个缩进问题(我是python的新手,我整天都在看代码,但没有看到!),应该在for循环完成之后才创建数据帧。引起问题。

答案 1 :(得分:0)

仅当密钥不可用时才会出现。

因此,要解决该问题,您可以为每个键设置一个If条件只是为了检查该键是否存在,或者可以使用KeyError异常通过异常处理来处理它

if:
  # code block
except KeyError, e:
  pass