KeyError:你没有名为0'的项目出现直方图

时间:2015-07-01 22:32:26

标签: python pandas matplotlib histogram keyerror

我在这里看过一些类似的问题,但似乎没有一个问题和我一样。我正在尝试创建化学数据的直方图。其他实例中的错误似乎与缺少的列有关,但我的数据没有(也不应该)有一个名为“0”的列。这是我的代码和错误消息:

%pylab inline
import matplotlib.pyplot as plt
import pandas as pd
plt.figure()

#importing the data
genesis = pd.read_csv(r'C:\Connors Temp\...... (878-15G)\Task_4 (Genesis)\genesis_MWMP.csv')

arsenic = genesis[['Code','Arsenic']]
antimony = genesis[['Code','Antimony']]
plt.hist(antimony)


KeyError                                  Traceback (most recent call last)
<ipython-input-7-c537deba42d9> in <module>()
----> 1 plt.hist(antimony)

C:\Python27\lib\site-packages\matplotlib\pyplot.pyc in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
   2655                       histtype=histtype, align=align, orientation=orientation,
   2656                       rwidth=rwidth, log=log, color=color, label=label,
-> 2657                       stacked=stacked, **kwargs)
   2658         draw_if_interactive()
   2659     finally:

C:\Python27\lib\site-packages\matplotlib\axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   8010         # Massage 'x' for processing.
   8011         # NOTE: Be sure any changes here is also done below to 'weights'
-> 8012         if isinstance(x, np.ndarray) or not iterable(x[0]):
   8013             # TODO: support masked arrays;
   8014             x = np.asarray(x)

C:\Python27\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
   1805                 raise ValueError('Cannot index using non-boolean DataFrame')
   1806         else:
-> 1807             return self._get_item_cache(key)
   1808 
   1809     def _getitem_array(self, key):

C:\Python27\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
    529             return cache[item]
    530         except Exception:
--> 531             values = self._data.get(item)
    532             res = self._box_item_values(item, values)
    533             cache[item] = res

C:\Python27\lib\site-packages\pandas\core\internals.pyc in get(self, item)
    828 
    829     def get(self, item):
--> 830         _, block = self._find_block(item)
    831         return block.get(item)
    832 

C:\Python27\lib\site-packages\pandas\core\internals.pyc in _find_block(self, item)
    942 
    943     def _find_block(self, item):
--> 944         self._check_have(item)
    945         for i, block in enumerate(self.blocks):
    946             if item in block:

    C:\Python27\lib\site-packages\pandas\core\internals.pyc in _check_have(self,    item)
    949     def _check_have(self, item):
    950         if item not in self.items:
--> 951             raise KeyError('no item named %s' % com.pprint_thing(item))
    952 
    953     def reindex_axis(self, new_axis, method=None, axis=0, copy=True):

KeyError: u'no item named 0'

2 个答案:

答案 0 :(得分:0)

hist采用一维数组。这有用吗?

antimony.Antimony.hist()

答案 1 :(得分:0)

如果你正在使用较低级别的库(也就是说,不是pandas&包装器),你可能应该使用

hist(antimony.Antimony.values)

(有关详情,请参阅hist文档。)