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

Viewed 6481

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'
2 Answers

restaurant_ratings_count does not contain a column called rating. It is a Series object, called rating, which has the count of the ratings above 4. If I understand your assignment, I believe you want

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

That gives you the names of the restaurants with more than 50 ratings. (They didn't ask for restaurants with more than 50 5-star ratings, which is what you were calculating.)

Followup

This produces the restaurant names that have both at least one 5 rating, and at least 50 ratings of any kind. I produce two Series and then merge them.

import pandas as pd

datain = [
        ["McDonalds", 3],
        ["McDonalds", 5],
        ["McDonalds", 5],
        ["McDonalds", 2],
        ["McDonalds", 4],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2],
        ["Carls", 3], ["Carls", 5], ["Carls", 5], ["Carls", 1], ["Carls", 2]
]
data = pd.DataFrame(datain, columns=['restaurant_name','rating'])
print(data)

five_star_ratings = data[data['rating']>4].drop_duplicates()['restaurant_name']
print(five_star_ratings)

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

more_than_50 = restaurant_ratings_count[restaurant_ratings_count>50]
print(more_than_50)

promo = pd.merge(five_star_ratings,more_than_50,left_on='restaurant_name',right_on='restaurant_name')
print(promo)


Output:

   restaurant_name  rating
0        McDonalds       3
1        McDonalds       5
2        McDonalds       5
3        McDonalds       2
4        McDonalds       4
5            Carls       3
6            Carls       5
7            Carls       5
8            Carls       1
9            Carls       2
10           Carls       3
11           Carls       5
12           Carls       5
13           Carls       1
14           Carls       2
15           Carls       3
16           Carls       5
17           Carls       5
18           Carls       1
19           Carls       2
20           Carls       3
21           Carls       5
22           Carls       5
23           Carls       1
24           Carls       2
25           Carls       3
26           Carls       5
27           Carls       5
28           Carls       1
29           Carls       2
30           Carls       3
31           Carls       5
32           Carls       5
33           Carls       1
34           Carls       2
35           Carls       3
36           Carls       5
37           Carls       5
38           Carls       1
39           Carls       2
40           Carls       3
41           Carls       5
42           Carls       5
43           Carls       1
44           Carls       2
45           Carls       3
46           Carls       5
47           Carls       5
48           Carls       1
49           Carls       2
50           Carls       3
51           Carls       5
52           Carls       5
53           Carls       1
54           Carls       2
55           Carls       3
56           Carls       5
57           Carls       5
58           Carls       1
59           Carls       2
1    McDonalds
6        Carls
Name: restaurant_name, dtype: object
restaurant_name
Carls        55
McDonalds     5
Name: rating, dtype: int64
restaurant_name
Carls    55
Name: rating, dtype: int64
  restaurant_name  rating
0           Carls      55

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

Related