1

i am trying to use this ready code i found it online on github

I installed all the packages:

pip install NRCLex
pip install textblob
python -m textblob.download_corpora

And this is my code:

from nrclex import NRCLex

import numpy as np
import pandas as pd

emotion = NRCLex(text)

df = pd.read_csv('test.csv')

df['emotions'] = df['text'].apply(lambda x: NRCLex(x).affect_frequencies) 

I face this error i this last line:

KeyError  Traceback (most recent call last)
File \~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3802, in Index.get_loc(self, key, method, tolerance)
3801 try:
\-\> 3802     return self.\_engine.get_loc(casted_key)
3803 except KeyError as err:

File \~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas_libs\\index.pyx:138, in pandas.\_libs.index.IndexEngine.get_loc()

File \~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas_libs\\index.pyx:165, in pandas.\_libs.index.IndexEngine.get_loc()

File pandas_libs\\hashtable_class_helper.pxi:5745, in pandas.\_libs.hashtable.PyObjectHashTable.get_item()

File pandas_libs\\hashtable_class_helper.pxi:5753, in pandas.\_libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'text'



The above exception was the direct cause of the following exception:



KeyError                                  Traceback (most recent call last)
Cell In\[39\], line 1
\----\> 1 df\['emotions'\] = df\['text'\].apply(lambda x: NRCLex(x).affect_frequencies)
2 df.head()


File \~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:3807, in DataFrame.__getitem__(self, key)
3805 if self.columns.nlevels \> 1:
3806     return self.\_getitem_multilevel(key)
\-\> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809     indexer = \[indexer\]


File \~\\AppData\\Local\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:3804, in Index.get_loc(self, key, method, tolerance)
3802     return self.\_engine.get_loc(casted_key)
3803 except KeyError as err:
\-\> 3804     raise KeyError(key) from err
3805 except TypeError:
3806     # If we have a listlike key, \_check_indexing_error will raise
3807     #  InvalidIndexError. Otherwise we fall through and re-raise
3808     #  the TypeError.
3809     self.\_check_indexing_error(key)

KeyError: 'text' 
8
  • Please format your code. In order to show actualy code, unlike codeproject, this site uses triple backticks before and after the intended code. For example , this is single line code (I used a single `)and this is multi-line code(I used three `) Commented Jul 25, 2023 at 7:37
  • I did exactly like the code in the link i provided, but her i wrote for you the specific error i faced, i did actually multi-line code Commented Jul 25, 2023 at 11:41
  • @literallyGutsFromBerserk Commented Jul 25, 2023 at 11:47
  • It's just saying there is no column 'text' in your dataframe. What does test.csv look like? Show is the first couple of lines. Commented Jul 25, 2023 at 19:28
  • Try finding out which keys the dataframe has by print(data.columns) Commented Jul 25, 2023 at 19:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.