Search code examples
pythonpandaskeyerror

How to Fix 'Key Errors' in pandas lib


I'm trying to import a csv file, using the pandas library. And then I want to create a data frame with two columns of the original file. One I want to name is volume and other trades. The 'preco_ordem' column is the one I want to turn into trades. But the following error happens: KeyError: 'preco_ordem'

I've changed the name of the variables and it worked with the volume 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)

This is my data

                 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

This is my error

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'

Solution

  • Check your column name using below:

    
    all_trades.columns