Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError if is_scalar(key) and isna(key) and not self.hasnans:

Tags:

python

pandas

Hello everyone I am new to Python and I am in a Data Analytics course and am having trouble with one of my problems. The following is the problem, my code, then the error message.

Problem: The company wants to provide a promotional offer in the advertisement of the restaurants. The condition to get the offer is that the restaurants must have a rating count of more than 50 and the average rating should be greater than 4. Find the restaurants fulfilling the criteria to get the promotional offer.

data

five_star_ratings = data.loc[data['rating']>4]

restaurant_ratings_count = five_star_ratings.groupby(['restaurant_name'])['rating'].count()

promo = restaurant_ratings_count[restaurant_ratings_count['rating']>50].count()

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

5 frames
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: 'rating'

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

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'rating'
like image 422
James Kuykendall Avatar asked Oct 17 '25 06:10

James Kuykendall


1 Answers

reset index... df.reset_index(inplace = True,drop = True)

like image 186
Selvam Avatar answered Oct 19 '25 21:10

Selvam