Как исправить «ключевые ошибки» в pandas lib - PullRequest
0 голосов
/ 30 мая 2019

Я пытаюсь импортировать 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 голосов
/ 30 мая 2019

Проверьте название столбца, используя ниже:


all_trades.columns
...