【问题标题】:Why am I getting "KeyError: 'Context'" in Python 3.8 (Anaconda/Spyder)为什么我在 Python 3.8 (Anaconda/Spyder) 中得到“KeyError: 'Context'”
【发布时间】:2021-01-03 21:08:13
【问题描述】:

我正在尝试测试一个函数来规范化文本,我相信这是我在 AI 聊天机器人 (https://medium.com/swlh/a-chatbot-in-python-using-nltk-938a37a9eacc) 上的“涉及的步骤”部分下关注的教程,但我不断收到 KeyError: 'Context'当我尝试将教程中的这一行复制到 Spyder 中时。 我已经尝试研究并再次阅读本教程并仔细检查我的库以查看我是否遗漏了任何内容,但我仍然没有弄清楚为什么缺少密钥所以我希望这里有人可以帮忙?

我的代码

import pandas as pd
import nltk
from nltk import pos_tag # for parts of speech
from nltk import word_tokenize # to create tokens
from nltk.stem import wordnet # to perform lemmatization
from nltk.corpus import stopwords # for stop words to end prgrm
import numpy as np
import re
from sklearn.metrics import pairwise_distances # to perform cosine similarity
from sklearn.feature_extraction.text import TfidfVectorizer # to perform tfidf
from sklearn.feature_extraction.text import CountVectorizer # to perform bow

df=pd.read_excel(r'C:\Users\mecha\Documents\Comp Sci - Year 3\ISYS30221 - Artificial Intel\New Try - AI with revisions\dialog_talk_agent.xlsx') # excel file of predetermined questions and answers


df.ffill(axis = 0, inplace=True) # fills all null values with previous value in dataset (NaN = null values)

df1 = df.head(10)

def step1(x):
    for i in x:
        a=str(i).lower()
        p=re.sub(r'[^a-z0-9]', ' ', a)
        print(p)

代码 sn -p 我在运行之前的代码后在控制台运行

step1(df1['Context'])

控制台中的错误反馈

Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)

  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'Context'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-8-6335e79211e5>", line 1, in <module>
    step1(df1['Context'])

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'Context'

我对 KnowledgeHut 进行了研究,我发现 KeyError 是因为我的程序找不到“上下文”键,但我一直在关注最近的教程密切相关,所以我不知道为什么会出现错误,或者可能是因为我缺少一些库? 我希望这里有人可以帮助我,同时我尝试学习一些基础知识,然后再开始我的学校 AI 聊天机器人项目。

【问题讨论】:

    标签: python-3.x regex pandas


    【解决方案1】:

    如果您在页面顶部查看 excel 文件的列名“上下文”和“文本响应”,那么您可能会在文件中遗漏这些名称或拼写错误,这是它不起作用的唯一方法

    【讨论】:

      【解决方案2】:

      step1(df1) 应该可以正常工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-28
        • 2017-05-24
        • 2017-07-09
        • 2021-04-02
        • 2023-01-23
        • 2021-09-30
        • 1970-01-01
        相关资源
        最近更新 更多