gpt4 book ai didi

python - KeyError 如果 is_scalar(key) 和 isna(key) 而不是 self.hasnans :

转载 作者:行者123 更新时间:2023-12-05 03:25:41 26 4
gpt4 key购买 nike

大家好,我是 Python 的新手,我正在学习数据分析类(class),但我遇到了一个问题。下面是问题,我的代码,然后是错误信息。

问题:公司想在餐厅的广告中提供促销优惠。获得优惠的条件是餐厅的评分必须超过50,平均评分必须大于4。找到符合条件的餐厅以获得促销优惠。

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'

最佳答案

重置索引... df.reset_index(inplace = True,drop = True)

关于python - KeyError 如果 is_scalar(key) 和 isna(key) 而不是 self.hasnans :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71929705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com