0

I am trying to build a chatbot in rasa. But my class ActionSearchRestaurants is throwing this error:

for index, row in resrnt.iterrows(): AttributeError: 'NoneType' object has no attribute 'iterrows'

Here is my class ActionSearchRestaurants that I'm using.

class ActionSearchRestaurants(Action):
    def name(self):
        return 'action_restaurant'

    def run(self, dispatcher, tracker, domain):
        config={ "user_key":"16cde****e0a12d10a7bc8bff6568031"}
        zomato = zomatopy.initialize_app(config)
        loc = tracker.get_slot('location')
        cuisine = tracker.get_slot('cuisine')
        location_detail=zomato.get_location(loc, 1)

        cols = ['restaurant_name', 'restaurant_address', 'avg_budget_for_two', 'zomato_rating']
        resrnt = pd.DataFrame(columns = cols)

        d1 = json.loads(location_detail)
        lat=d1["location_suggestions"][0]["latitude"]
        lon=d1["location_suggestions"][0]["longitude"]
        cuisines_dict={'bakery':5,'chinese':25,'cafe':30,'italian':55,'biryani':7,'north indian':50,'south indian':85}
        results=zomato.restaurant_search("", lat, lon, str(cuisines_dict.get(cuisine)), 10)
        d = json.loads(results)
        response=""
        if d['results_found'] == 0:
            response= "no results"
        else:
            for restaurant in d['restaurants']:
                curr_res = {'zomato_rating':restaurant['restaurant']["user_rating"]["aggregate_rating"],'restaurant_name':restaurant['restaurant']['name'],'restaurant_address': restaurant['restaurant']['location']['address'], 'avg_budget_for_two': restaurant['restaurant']['average_cost_for_two']}
                resrnt = resrnt.append(curr_res, ignore_index=True)

        resrnt=resrnt.sort_values(by=['zomato_rating'], ascending=False, inplace=True)

        for index, row in resrnt.iterrows():
                response = response+ index + ". Found \""+ row['restaurant_name']+ "\" in "+ row['restaurant_address']+" has been rated "+ row['zomato_rating']+"\n"

        dispatcher.utter_message("-----"+ response)
        return [SlotSet('location',loc)]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Xena_psk
  • 25
  • 8
  • Does this answer your question? [Why do I get AttributeError: 'NoneType' object has no attribute 'something'?](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) – Ulrich Eckhardt Jan 05 '22 at 18:11

1 Answers1

2

On this line:

resrnt=resrnt.sort_values(by=['zomato_rating'], ascending=False, inplace=True)

From the documentation for Dataframe.sort_values:

Returns: sorted_obj : DataFrame or None

DataFrame with sorted values if inplace=False, None otherwise.

Since inplace=True, the DataFrame is replaced with None, which of course does not have a .iterrows.

Either use inplace=False (or omit it), or do not reassign. (One specific reason for using inplace=True is so as not to need the reassignment.)

Community
  • 1
  • 1
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thankyou for your response. I omitted inplace from my code. Attribute Error is gone but now I'm getting this error response = response+ index + ". Found \""+ row['restaurant_name']+ "\" in "+ row['restaurant_address']+" has been rated "+ row['zomato_rating']+"\n" TypeError: must be str, not int – Xena_psk Oct 20 '19 at 03:47
  • Either cast the integer values to string using `str()` or use string formatting. – adrianp Oct 20 '19 at 03:49