Error: pandas hashtable keyerror
I think first is best investigate, what are real columns names, if convert to list better are seen some whitespaces or similar:
print (reviews_new.columns.tolist())
I think there can be 2 problems (obviously):
1.whitespaces in columns names (maybe in data also)
Solutions are strip
whitespaces in column names:
reviews_new.columns = reviews_new.columns.str.strip()
Or add parameter skipinitialspace
to read_csv
:
reviews_new = pd.read_csv("D:\\aviva.csv", skipinitialspace=True)
2.different separator as default ,
Solution is add parameter sep
:
#sep is ;
reviews_new = pd.read_csv("D:\\aviva.csv", sep=';')
#sep is whitespace
reviews_new = pd.read_csv("D:\\aviva.csv", sep='\s+')
reviews_new = pd.read_csv("D:\\aviva.csv", delim_whitespace=True)
EDIT:
You get whitespace in column name, so need 1.solutions
:
print (reviews_new.columns.tolist())
['Name', ' Date', ' review']
^ ^
Pandas Hashtable KeyError
The following lines
X = df['MaterialDescription']
clean_review = description_to_words(X[3] )
give in python description_to_words(df['MaterialDescription'][3] )
You have to locate your index through:
clean_review = description_to_words(df.iloc[3]['MaterialDescription'] )
pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 0
I think this error comes from your [i]
notation, which is trying to look for the DataFrame index value of 0, which doesn't exist. Try replacing every instance of [i]
with .iloc[i]
.
Also, you should be able to replace the for loop with much more compact, readable, and less error-prone code, especially since you define emotion_map
but use it only for output. Try changing the mapping from strings to integers with emotion_map = { 0:6, 1:3, 2:4, 3:5, 4:2, 5:1, 6:0}
, then move it to just under filtered_csv = ...
, and replace that for
loop with
filtered_csv['expression'] = filtered_csv['expression'].replace(emotion_map)
Related Topics
Executable Python Program with All Dependencies for Linux
Storing Value from a Parsed Ping
Pytest - Specify Log Level from the Cli Command Running the Tests
How to Generate Dynamic (Parameterized) Unit Tests in Python
Iterating on a File Doesn't Work the Second Time
Understanding _Get_ and _Set_ and Python Descriptors
How to Find the Time Difference Between Two Datetime Objects in Python
How to Include a Folder with Cx_Freeze
Tensorflow Not Found Using Pip
Check If a Word Is in a String in Python
Set Bash Variable from Python Script
To Read Line from File Without Getting "\N" Appended at the End
Calling Parent Class _Init_ with Multiple Inheritance, What's the Right Way
Pg_Config Executable Not Found