繁体   English   中英

如何使用Seaborn和Pandas数据集修复Barplot错误(它不会让我对变量进行barplot)

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

我正在尝试绘制由熊猫数据集(python 3)制成的变量,以显示按国家/地区排名前5位的星级。 我不确定什至可以尝试不同的方法,因为它可以在整个数据帧中正常工作,而不是在我的变量中工作。 伙计们,首先在这里发布文章,如果我没有提供足够的信息,对不起!

适用于折线图,在我的整个数据框中,折线图也很好

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'

```````

遇到此类问题时,请始终创建一个最小的示例 所以在这里看起来像

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"])将导致KeyError 为什么? 因为当您打印分组的系列print(g)

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

您会注意到

  1. 这是一个系列,而不是数据框。 因此,索引将选择系列中的项目,而不是列。
  2. "X"只是索引的名称。 因此,您要寻找的是

     g.index 

因此

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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