I am trying to barplot a variable I made of a pandas data set (python 3) to show the top 5 star ratings by country. I am unsure what to even try differently because it works fine with the full dataframe, just not my variable. First post here guys, sorry if I did not put enough information!
Works fine for a line plot, and barplots just fine in my full dataframe
import pandas as pd, numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
ramen = pd.read_csv('D:/Statistics/Stats Projects/Ramen/cleaner_ramen_ratings.csv')
sorted_group = ramen.groupby('Country')['Stars'].mean().sort_values(ascending=False)
top_ten_countries = sorted_group.head(10)
plt.figure(figsize = (12,6))
plt.title('Top Five Ramen Ratings by Country')
sns.barplot(x=top_ten_countries["Country"], y=top_ten_countries["Stars"])
TypeError Traceback (most recent call last)
d:\python\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4379 try:
-> 4380 return libindex.get_value_box(s, key)
4381 except IndexError:
pandas\_libs\index.pyx in pandas._libs.index.get_value_box()
pandas\_libs\index.pyx in pandas._libs.index.get_value_at()
pandas\_libs\util.pxd in pandas._libs.util.get_value_at()
pandas\_libs\util.pxd in pandas._libs.util.validate_indexer()
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-83-ad5d747081eb> in <module>
3 plt.title('Top Five Ramen Ratings by Country')
4
----> 5 sns.barplot(x=top_ten_countries["Country"], y=top_ten_countries["Stars"])
d:\python\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
--> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):
d:\python\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4386 raise InvalidIndexError(key)
4387 else:
-> 4388 raise e1
4389 except Exception: # pragma: no cover
4390 raise e1
d:\python\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4372 try:
4373 return self._engine.get_value(s, k,
-> 4374 tz=getattr(series.dtype, 'tz', None))
4375 except KeyError as e1:
4376 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
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: 'Country'
```````