子集数据的plt.hist错误

时间:2015-05-14 18:04:51

标签: python python-3.x pandas seaborn

我对Python很陌生,所以请原谅我的任何愚蠢。

我正在使用matplotlib运行直方图并在使用子集数据时出错,如果我使用完整数据集,代码可以正常工作,因此我感到困惑。

也许我没有正确分组?

我的代码如下,相关错误如下,谢谢。

  • 为了提高认识,这是用Python 3编写的。

导入所需的包:

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

阅读数据:

mlb=pd.read_csv('C:\Users\ocmh\Desktop\Python\Batting.csv')

查看数据样本:

mlb.head()

将数据子集以返回波士顿数据:

mlb_bos=mlb[(mlb['teamID'] == 'BOS')]

查看子集数据的示例:

mlb_bos.head()

绘制原始数据的直方图:并且效果很好

plt.hist(mlb.AB.dropna, color= sns.desaturate("indianred",1))

绘制子集数据的直方图:返回错误(错误在下面)

plt.hist(mlb_bos.AB.dropna, color= sns.desaturate("indianred",1))

如果你没有安装seaborn包,你可以放弃color = sns.desaturate(“indianred”,1),因为这纯粹是为了美观。

以下错误:

KeyError                                  Traceback (most recent call last)

<ipython-input-11-1484047d7ac6> in <module>()

----> 1 plt.hist(mlb_bos.AB, color=color)



/Users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)

   2894                       histtype=histtype, align=align, orientation=orientation,

   2895                       rwidth=rwidth, log=log, color=color, label=label,

-> 2896                       stacked=stacked, **kwargs)

   2897         draw_if_interactive()

   2898     finally:



/Users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)

   5602         # Massage 'x' for processing.

   5603         # NOTE: Be sure any changes here is also done below to 'weights'

-> 5604         if isinstance(x, np.ndarray) or not iterable(x[0]):

   5605             # TODO: support masked arrays;

   5606             x = np.asarray(x)



/Users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/series.py in __getitem__(self, key)

    512     def __getitem__(self, key):

    513         try:

--> 514             result = self.index.get_value(self, key)

    515

    516             if not np.isscalar(result):



/Users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/index.py in get_value(self, series, key)

   1458

   1459         try:

-> 1460             return self._engine.get_value(s, k)

   1461         except KeyError as e1:

   1462             if len(self) > 0 and self.inferred_type in ['integer','boolean']:



pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3113)()



pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2844)()



pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()



pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7255)()



pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:7193)()



KeyError: 0

0 个答案:

没有答案
相关问题