【问题标题】:When I applied (NLTK) stop words to a data frame it showing an error?当我将(NLTK)停用词应用于数据框时,它显示错误?
【发布时间】:2019-04-28 10:37:58
【问题描述】:
  Reviews                                               Label
0   Bromwell High is a cartoon comedy. It ran at t...   Positive
1   Homelessness (or Houselessness as George Carli...   Positive
2   Brilliant over-acting by Lesley Ann Warren. Be...   Positive

上面是我的数据框,列:评论和标签当我执行下面的代码时:`

nltk.download('stopwords') This is used to update stop words.
from nltk.corpus import stopwords
stop = stopwords.words('english')
final_without_stopwords = final[['Reviews','Label']].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)])).str.replace('[^\w\s]','')
print(final_without_stopwords)`

结果:

KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Reviews', 'Label')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-52-cb4ca290db84> in <module>()
      5 #final['Reviews'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop_words)]))
      6 
----> 7 final_without_stopwords = final['Reviews','Label'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)])).str.replace('[^\w\s]','')
      8 print(final_without_stopwords)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Reviews', 'Label')
    enter code here

**

实际上,我想将停用词应用于只有两列的数据框。当我用单列(评论)执行此代码时,它运行良好 但是当我用两列(评论和标签)执行时,它显示 一些错误。任何关于如何处理这两列代码的建议。

**

【问题讨论】:

  • 你的意思是像final['Reviews_n'] = final[[Reviews'].apply(lambda x: ' '.join...这样的事情吗?为什么要从 Label 中删除停用词?
  • @BernardL 先生,在上面的数据框中只有两列,但是如果有多列,如何将 Stop Words 应用于所有列?我实际上用 Label 列进行了测试,但它抛出了一个错误。我想处理多个列。先生可以吗?
  • 是的,有可能,参考下面我的回答。

标签: python python-3.x jupyter-notebook nltk nltk-trainer


【解决方案1】:

如果您想将函数元素应用于数据框,请使用applymap

一个简化的例子:

import pandas as pd

stop = set(['a','the','i','is'])
df = pd.DataFrame( {'sentence1':['i am a boy','i am a girl'],
                    'sentence2':['Bromwell High is a cartoon comedy','i am a girl']})

df[['sentence1','sentence2']].applymap(lambda x: ' '.join(i for i in x.split() if i not in stop))


   sentence1    sentence2
0   am boy       Bromwell High cartoon comedy
1   am girl      am girl

如果您想将不带停用词的值重新分配到数据框中,请使用:

df[['sentence1','sentence2']] = df[['sentence1','sentence2']].applymap(lambda x: ' '.join(i for i in x.split() if i not in stop))

【讨论】:

  • 先生,它工作得很好。太感谢了。如果您能推荐好的资源来深入学习python知识,那将非常有帮助。
  • 阅读文档。对于PythonPandas,只是阅读它可能有点枯燥,但请配合它并在编码时始终打开它们。
猜你喜欢
  • 2022-10-15
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多