Pandas hashtable keyerror in Python

The error: Pandas hashtable keyerror in Python is a very popular error, the error happens when the developer adds some faulty characters that we will investigate in this post.

This article presents the explanationof the problem and the solution that worked for me, plus other solutions that I discovered along the way.

Explaining The Error : Pandas hashtable keyerror in Python

I have successfully read a csv file using pandas. When I am trying to print the a particular column from the data frame i am getting keyerror. Hereby i am sharing the code with the error.

I encountered this error after reading a csv file using pandas with Python, the error occurs when I try to print a certain column, it does not matter which column, I always get the same error.

This is my code

                                                                       #
import pandas as pd
reviews_new = pd.read_csv("C:\\home.csv")
reviews_new['review']
                                                                       #

This is the error Im getting

                                                                       #
reviews_new['review']
Traceback (most recent call last):
  File "<ipython-input-43-ed485b439a1c>", line 1, in <module>
    reviews_new['review']
(pandas\hashtable.c:12322)
KeyError: 'review'
                                                                       #

After hours of research, I finally fixed the error, let us see all the possible solution to this problem.

Solution 1 : Remove whitespaces from column names

First we need to run the code bellow:

                                                                       #
print (reviews_new.columns.tolist())
                                                                       #

The goal from running the code above is to investigate the columns names, we need to know if we have whitespaces or other things that are causing the error.

In our example, this is what we get :

                                                                                           #
print (reviews_new.columns.tolist())
['Age', ' Name', ' Gendee'] 
          ^        ^                                                                       #

Notice the presence of the whitespaces, that is exactly why we have the error.

The solution is to remove the whitespaces using strip, for example

                                                                       #
reviews_new.columns = reviews_new.columns.str.strip()
                                                                       #

The other solution is to add the skipinitialspace parameter to read_csv:

                                                                       #
reviews_new = pd.read_csv("D:\\aviva.csv", skipinitialspace=True)
                                                                       #

The solution above will work for most people, if you still have the same error then, please read the next solution.

Solution 2 : Add parameter “sep

If you do not have whitespaces then you must have different separator as default,

there is only one solution to the problem, the solution is to add parameter sep

The code bellow is a clear demonstration of the solution

                                                                       #
#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)
                                                                       #

I hope this solution helped you, I can not find any other solution or cause for this error. Except the ones I provided above.

Summing-up

That is it guys, we arrived at the end of this article, I did my best to help you solve this issue, I wish you good luck with your Python projects and I wish you good luck with the errors to come, Python is dark and full of errors. But, It is our Favourite Programming Language, Cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/