如何修复Pandas lib中的“关键错误”

时间:2019-05-30 12:14:02

标签: python pandas keyerror

我正在尝试使用pandas库导入一个csv文件。然后我想用原始文件的两列创建一个数据框。我想说的是交易量和其他交易。 'preco_ordem'列是我想进行的交易。但是会发生以下错误:KeyError:'preco_ordem'

我已经更改了变量的名称,并且它与体积data.frame一起使用。

from itertools import zip_longest
import itertools
import pandas
import numpy as np
import matplotlib.pyplot as plt

all_trades = pandas.read_csv('./ccmx18hf .csv', parse_dates={'Date': [0,1]}, index_col=0, encoding = 'unicode_escape')
print (all_trades.head())


volume = (all_trades['quant_total'])
print(volume.head())
trades = (all_trades['preco_ordem'])

def cleanup(x):
    if isinstance(x, str) and 'e-' in x:
        return 0
    else:
        return float(x)

volume = volume.apply(lambda x: cleanup(x))
volume = volume.astype(np.float32)

这是我的数据

                 order_side pre�o_ordem  quant_total  quant_neg status
Date                                                                     
02/07/2018 08:58:25   Sell       39,36            4        0.0   New
02/07/2018 08:59:53   Sell          40            1        0.0   New
02/07/2018 08:59:56   Sell       39,93            1        0.0   New
02/07/2018 09:00:22    Buy        39,1           10        0.0   New
02/07/2018 09:00:26    Buy       39,11            1        0.0   New
Date
02/07/2018 08:58:25     4
02/07/2018 08:59:53     1
02/07/2018 08:59:56     1
02/07/2018 09:00:22    10
02/07/2018 09:00:26     1
Name: quant_total, dtype: int64

这是我的错误

Traceback (most recent call last):
  File "/home/operacao/miniconda2/envs/py3env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2657, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'preco_ordem'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ccmx.py", line 15, in <module>
    trades = (all_trades['preco_ordem'])
  File "/home/operacao/miniconda2/envs/py3env/lib/python3.7/site-packages/pandas/core/frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/operacao/miniconda2/envs/py3env/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'preco_ordem'

1 个答案:

答案 0 :(得分:0)

使用以下方法检查您的列名:


all_trades.columns