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'
this is single line code(I used a single `)andthis is multi-line code(I used three `)'text'in your dataframe. What doestest.csvlook like? Show is the first couple of lines.