【发布时间】:2021-10-29 03:00:33
【问题描述】:
我是 Python 新手,目前正在尝试编写一个代码,它会自动建议特定问题的答案。我在运行以下代码时遇到了这个问题:
import pandas as pd
df=pd.read_csv("Book11.csv", encoding= 'cp1252');
df.columns=["question","answers"]
df
print(df)
import re
import gensim
from gensim.parsing.preprocessing import remove_stopwords
def clean_sentence(sentence,stopwords=False):
sentence = sentence.lower().strip()
sentence = re.sub(r'[^a-z0-9\s]','',sentence)
if stopwords:
sentence = remove_stopwords(sentence)
return sentence
def get_cleaned_sentences (df, stopwords=False):
sents=df[["questions"]];
cleaned_sentences=[]
for index,row in df.iterrows():
#print(index.row)
cleaned=clean_sentence(row["questions"], stopwords);
cleaned_sentences.append(cleaned);
return cleaned_sentences;
cleaned_sentences=get_cleaned_sentences(df, stopwords=True)
print(cleaned_sentences);
-在 Colab 上运行时 - 运行良好 - 在 Windows 下的本地 Python 3.9.1 上运行时 - 它工作正常 - 在 Ubuntu VM 上运行时,运行相同的代码只会给我以下错误:KeyError: "None of [Index(['questions'], dtype='object')] are in the [columns]"
我已经尝试了搜索上述错误后找到的所有解决方法,但没有成功。
我不明白为什么这可以在两个环境中无缝运行。
非常感谢。
【问题讨论】: