简体   繁体   中英

How to fix Barplot errors using Seaborn with a Pandas Data Set (It Will not let me barplot my variable)

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'

```````

Always create a minimal example when running into such problems. So here it might look like

import numpy as np
import pandas as pd

df = pd.DataFrame({"X" : np.repeat(list("ABCD"), 50),
                   "Y" : np.cumsum(np.random.randn(200))})

g = df.groupby("X")["Y"].mean()

print(g["X"]) will result in a KeyError . Why? Because when you print the grouped series print(g) ,

X
A   -0.308931
B   -0.711863
C    0.647343
D    3.752564
Name: Y, dtype: float64

you'll notice that

  1. It's a series, not a dataframe. Hence indexing will select items of the series, not columns.
  2. "X" is just the name of the index. Therefore what you're looking for is

     g.index 

and hence

sns.barplot(x=g.index, y=g)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM