Python教程帮助NLP客户评论

时间:2019-09-24 16:58:40

标签: python-3.x pandas dataframe nlp nltk

我对Python还是很陌生,并且正在学习有关根据客户评论文件创建wordcloud的教程。教程链接为https://towardsdatascience.com/detecting-bad-customer-reviews-with-nlp-d8b36134dc7e

from wordcloud import WordCloud, STOPWORDS
import pandas as pd

# read data
reviews_df = pd.read_csv("Hotel_Reviews3.csv")
# append the positive and negative text reviews
reviews_df["review"] = reviews_df["Negative_Review"] + reviews_df["Positive_Review"]

# create the label
reviews_df["is_bad_review"] = reviews_df["Reviewer_Score"].apply(lambda x: 1 if x < 5 else 0)

# select only relevant columns
reviews_df = reviews_df[["review", "is_bad_review"]]
reviews_df.head()

Hotel_Reviews3.csv:   https://i.stack.imgur.com/8ZGxj.png

错误消息

Traceback (most recent call last):
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Positive_Review'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\stecd\Desktop\WorldCloud\wordCloud.py", line 6, in <module>
    reviews_df["review"] = reviews_df["Negative_Review"] + reviews_df["Positive_Review"]
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Users\stecd\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Positive_Review'
>>> 

1 个答案:

答案 0 :(得分:0)

从错误消息中,我猜测Hotel_Reviews3.csv可能没有"Positive_Review"列。可能是相应的表条目被截断或具有空格,因此与"Positive_Review"不匹配。