当列清楚存在时,matplotlib中的Keyerror

时间:2016-04-28 15:48:49

标签: python pandas plot

我正在尝试使用以下代码进行绘图:

df.plot(kind='scatter',x='branch', y='retention', s=df['active_users']*200)

这给了我以下错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-17-e43e5aeff662> in <module>()
      3 df = Flexbooks[Flexbooks['schoolyearsemester'] == StartSem][Flexbooks['branch'] != 'OTHE'][Flexbooks['branch'] != 'SSCI'][Flexbooks['branch'] != 'EM1'][Flexbooks['branch'] != 'EM2'][Flexbooks['branch'] != 'EM3'][Flexbooks['branch'] != 'EM4'][Flexbooks['branch'] != 'EM5'][Flexbooks['branch'] != 'SATP'][Flexbooks['branch'] != 'MORE'][Flexbooks['branch'] != 'SPEL'][Flexbooks['branch'] != 'ENG'][Flexbooks['branch'] != 'ENGG'][Flexbooks['branch'] != 'NANO'][Flexbooks['branch'] != 'TECH'][Flexbooks['branch'] != 'HIST'][Flexbooks['branch'] != 'WRIT'][Flexbooks['branch'] != 'ASTR'][Flexbooks['branch'] != 'EXAP']
      4 
----> 5 df.plot(kind='scatter',x='branch', y='retention', s=df['active_users']*200)
      6 #df.plot.scatter(x='branch', y='retention', s=df['active_users']*200)

E:\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   3669                           fontsize=fontsize, colormap=colormap, table=table,
   3670                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3671                           sort_columns=sort_columns, **kwds)
   3672     __call__.__doc__ = plot_frame.__doc__
   3673 

E:\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2554                  yerr=yerr, xerr=xerr,
   2555                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 2556                  **kwds)
   2557 
   2558 

E:\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in _plot(data, x, y, subplots, ax, kind, **kwds)
   2382         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   2383 
-> 2384     plot_obj.generate()
   2385     plot_obj.draw()
   2386     return plot_obj.result

E:\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in generate(self)
    985         self._compute_plot_data()
    986         self._setup_subplots()
--> 987         self._make_plot()
    988         self._add_table()
    989         self._make_legend()

E:\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in _make_plot(self)
   1556         else:
   1557             label = None
-> 1558         scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
   1559                              label=label, cmap=cmap, **self.kwds)
   1560         if cb:

E:\Anaconda2\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
   1967             return self._getitem_multilevel(key)
   1968         else:
-> 1969             return self._getitem_column(key)
   1970 
   1971     def _getitem_column(self, key):

E:\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _getitem_column(self, key)
   1974         # get column
   1975         if self.columns.is_unique:
-> 1976             return self._get_item_cache(key)
   1977 
   1978         # duplicate columns & possible reduce dimensionality

E:\Anaconda2\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
   1089         res = cache.get(item)
   1090         if res is None:
-> 1091             values = self._data.get(item)
   1092             res = self._box_item_values(item, values)
   1093             cache[item] = res

E:\Anaconda2\lib\site-packages\pandas\core\internals.pyc in get(self, item, fastpath)
   3209 
   3210             if not isnull(item):
-> 3211                 loc = self.items.get_loc(item)
   3212             else:
   3213                 indexer = np.arange(len(self.items))[isnull(self.items)]

E:\Anaconda2\lib\site-packages\pandas\core\index.pyc in get_loc(self, key, method, tolerance)
   1757                                  'backfill or nearest lookups')
   1758             key = _values_from_object(key)
-> 1759             return self._engine.get_loc(key)
   1760 
   1761         indexer = self.get_indexer([key], method=method,

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)()

pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12265)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12216)()

KeyError: 'branch'

我可以自信地说列'分支'存在,那么为什么KeyError

我知道datetime中的列时存在此问题,但此列为string

任何帮助都将不胜感激。

0 个答案:

没有答案