【问题标题】:How to Fix 'Key Errors' in pandas lib如何修复 pandas lib 中的“关键错误”
【发布时间】:2019-05-30 12:14:02
【问题描述】:

我正在尝试使用 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'

【问题讨论】:

  • 这意味着 python 无法找到“preco_ordem”列。使用“all_trades.columns”检查您的列名。好像写成preÂ�o_ordem

标签: python pandas keyerror


【解决方案1】:

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


all_trades.columns

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 2021-04-08
    • 2021-08-24
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多