19
Фев
2018

Вылетает ошибка "KeyError: ‘<column_name>’" при работе с pandas

import pandas as pd
from pathlib import Path

def read_all(files, **kwargs):
    return pd.concat([pd.read_csv(f, **kwargs) for f in files], ignore_index=True)

p = Path(r'C:/Users/red.iguana/PycharmProjects/nativePython/algorithm/csv/csv')

phones = [390004340]
df = read_all(p.glob('*.csv'))
print(df.query("A in @phones"))

программа должна искать номера в csv файлах После запуска вылезает такая ошибка.

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 181, in resolve
    return self.resolvers[key]
  File "C:\Program Files (x86)\Python36-32\lib\collections\__init__.py", line 885, in __getitem__
    return self.__missing__(key)            # support subclasses that define __missing__
  File "C:\Program Files (x86)\Python36-32\lib\collections\__init__.py", line 877, in __missing__
    raise KeyError(key)
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 192, in resolve
    return self.temps[key]
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/red.iguana/PycharmProjects/nativePython/algorithm/csv/main.py", line 11, in <module>
    print(df.query("A in @phones"))
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\frame.py", line 2297, in query
    res = self.eval(expr, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\frame.py", line 2366, in eval
    return _eval(expr, inplace=inplace, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\eval.py", line 290, in eval
    truediv=truediv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 732, in __init__
    self.terms = self.parse()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 749, in parse
    return self._visitor.visit(self.expr)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 320, in visit_Module
    return self.visit(expr, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 323, in visit_Expr
    return self.visit(node.value, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 649, in visit_Compare
    return self.visit(binop)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 418, in visit_BinOp
    op, op_class, left, right = self._maybe_transform_eq_ne(node)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 355, in _maybe_transform_eq_ne
    left = self.visit(node.left, side='left')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 432, in visit_Name
    return self.term_type(node.id, self.env, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\ops.py", line 58, in __init__
    self._value = self._resolve_name()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\ops.py", line 75, in _resolve_name
    res = self.env.resolve(self.local_name, is_local=self.is_local)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 194, in resolve
    raise compu.ops.UndefinedVariableError(key, is_local)
pandas.core.computation.ops.UndefinedVariableError: name 'A' is not defined

Источник: https://ru.stackoverflow.com/questions/787481/%D0%92%D1%8B%D0%BB%D0%B5%D1%82%D0%B0%D0%B5%D1%82-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0-keyerror-column-name-%D0%BF%D1%80%D0%B8-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B5-%D1%81-pandas

19
Фев
2018

Вылетает ошибка "KeyError: ‘<column_name>’" при работе с pandas

import pandas as pd
from pathlib import Path

def read_all(files, **kwargs):
    return pd.concat([pd.read_csv(f, **kwargs) for f in files], ignore_index=True)

p = Path(r'C:/Users/red.iguana/PycharmProjects/nativePython/algorithm/csv/csv')

phones = [390004340]
df = read_all(p.glob('*.csv'))
print(df.query("A in @phones"))

программа должна искать номера в csv файлах После запуска вылезает такая ошибка.

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 181, in resolve
    return self.resolvers[key]
  File "C:\Program Files (x86)\Python36-32\lib\collections\__init__.py", line 885, in __getitem__
    return self.__missing__(key)            # support subclasses that define __missing__
  File "C:\Program Files (x86)\Python36-32\lib\collections\__init__.py", line 877, in __missing__
    raise KeyError(key)
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 192, in resolve
    return self.temps[key]
KeyError: 'A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/red.iguana/PycharmProjects/nativePython/algorithm/csv/main.py", line 11, in <module>
    print(df.query("A in @phones"))
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\frame.py", line 2297, in query
    res = self.eval(expr, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\frame.py", line 2366, in eval
    return _eval(expr, inplace=inplace, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\eval.py", line 290, in eval
    truediv=truediv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 732, in __init__
    self.terms = self.parse()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 749, in parse
    return self._visitor.visit(self.expr)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 320, in visit_Module
    return self.visit(expr, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 323, in visit_Expr
    return self.visit(node.value, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 649, in visit_Compare
    return self.visit(binop)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 418, in visit_BinOp
    op, op_class, left, right = self._maybe_transform_eq_ne(node)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 355, in _maybe_transform_eq_ne
    left = self.visit(node.left, side='left')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 314, in visit
    return visitor(node, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\expr.py", line 432, in visit_Name
    return self.term_type(node.id, self.env, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\ops.py", line 58, in __init__
    self._value = self._resolve_name()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\ops.py", line 75, in _resolve_name
    res = self.env.resolve(self.local_name, is_local=self.is_local)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\computation\scope.py", line 194, in resolve
    raise compu.ops.UndefinedVariableError(key, is_local)
pandas.core.computation.ops.UndefinedVariableError: name 'A' is not defined

Источник: https://ru.stackoverflow.com/questions/787481/%D0%92%D1%8B%D0%BB%D0%B5%D1%82%D0%B0%D0%B5%D1%82-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0-keyerror-column-name-%D0%BF%D1%80%D0%B8-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B5-%D1%81-pandas

Тебе может это понравится...

Добавить комментарий