簡體   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