Python 列明显存在时matplotlib中的Keyerror
我尝试使用以下代码进行绘图:Python 列明显存在时matplotlib中的Keyerror,python,pandas,plot,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 = F
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'
keyrerror回溯(最近一次调用)
在()
3 df=Flexbooks[Flexbooks['SchoolYearSemmer']==StartSem][Flexbooks['branch']!='SSCI'][Flexbooks['branch']!='EM1'][Flexbooks['branch']!='EM2'][Flexbooks['branch']!='EM3'][Flexbooks['branch']!='EM4'][Flexbooks['branch branch']!='EM5'][Flexbooks['branch']!='branch']!='SATP'][Flexbooks['branch Flexbooks['['branch']!='SPEL'][Flexbooks['branch']!='ENG'][Flexbooks['branch']!='NANO'][Flexbooks['branch']!='TECH'][Flexbooks['branch']!='HIST'][Flexbooks['branch']!='write'][Flexbooks['branch']!='ASTR'][Flexbooks['branch']!='ASTR']!='EXAP']
4.
---->5 df.绘图(种类='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_uuuucall_uuuu(self、x、y、kind、ax、subplot、sharex、sharey、layout、figsize、use_index、title、grid、legend、style、logx、logy、loglog、xticks、yticks、xlim、ylim、rot、fontsize、colormap、table、yerr、xer、secondary_y、sort_u列、**kwds)
3669 fontsize=fontsize,colormap=colormap,table=table,
3670 yerr=yerr,xerr=xerr,secondary_y=secondary_y,
->3671排序列=排序列,**kwds)
3672 uuuu调用uuuu文件uuuu=绘图uu框架uuuuuuu文件__
3673
E:\Anaconda2\lib\site packages\pandas\tools\plotting.pyc在plot_框架中(数据、x、y、种类、ax、子地块、sharex、sharey、布局、figsize、使用索引、标题、网格、图例、样式、logx、logy、loglog、xticks、yticks、xlim、ylim、rot、fontsize、colormap、table、yerr、xerr、secondary__、sort_列、**kwds)
2554 yerr=yerr,xerr=xerr,
2555 secondary_y=secondary_y,sort_columns=sort_columns,
->2556**科威特第纳尔)
2557
2558
E:\Anaconda2\lib\site packages\pandas\tools\plotting.pyc in_plot(数据、x、y、子地块、ax、种类、**kwds)
2382绘图对象=klass(数据,子地块=子地块,ax=ax,种类=种类,**kwds)
2383
->2384绘图对象生成()
2385标绘对象图()
2386返回图\对象结果
E:\Anaconda2\lib\site packages\pandas\tools\plotting.pyc in generate(self)
985自我计算绘图数据()
986自安装子批次()
-->987自成图()
988自我添加表()
989自成传奇()
E:\Anaconda2\lib\site packages\pandas\tools\plotting.pyc in\u make\u plot(self)
1556其他:
1557标签=无
->1558散点=最大散点(数据[x]。值,数据[y]。值,c=c_值,
1559 label=label,cmap=cmap,**self.kwds)
1560如果cb:
E:\Anaconda2\lib\site packages\pandas\core\frame.pyc in\uuuuu getitem\uuuuuuu(self,key)
1967返回自我。\u获取项目\u多级(键)
其他:
->1969返回自我。\u获取项目\u列(键)
1970
1971 def_getitem_列(自身,键):
E:\Anaconda2\lib\site packages\pandas\core\frame.pyc在_getitem_列中(self,key)
1974年#获得专栏
1975如果self.columns.u是唯一的:
->1976返回自我。获取项目缓存(密钥)
1977
1978#重复列和可能的降维
E:\Anaconda2\lib\site packages\pandas\core\generic.pyc在\u get\u item\u缓存中(self,item)
1089 res=cache.get(项)
1090如果res为无:
->1091 values=self.\u data.get(项目)
1092 res=自身。_框_项_值(项,值)
1093缓存[项目]=res
get中的E:\Anaconda2\lib\site packages\pandas\core\internals.pyc(self、item、fastpath)
3209
3210如果不为空(项目):
->3211 loc=自身物品。获取物品位置(物品)
3212其他:
3213 indexer=np.arange(len(self.items))[isnull(self.items)]
E:\Anaconda2\lib\site packages\pandas\core\index.pyc in get\u loc(self、key、method、tolerance)
1757“回填或最近的查找”)
1758键=来自对象(键)的值
->1759返回发动机。获取位置(钥匙)
1760
1761 indexer=self.get\u indexer([key],method=method,
pandas.index.IndexEngine.get_loc中的pandas\index.pyx(pandas\index.c:3979)()
pandas.index.IndexEngine.get_loc中的pandas\index.pyx(pandas\index.c:3843)()
pandas.hashtable.PyObjectHashTable.get_项中的pandas\hashtable.pyx(pandas\hashtable.c:12265)()
pandas.hashtable.PyObjectHashTable.get_项中的pandas\hashtable.pyx(pandas\hashtable.c:12216)()
KeyError:“分支”
我可以自信地说“branch”列是存在的,那么为什么会出现KeyError
我知道当datetime
中的列存在此问题,但此列是string
任何帮助都将不胜感激。检查列名上的前导/尾随空格。我正在使用语句
df=pd.dataframe(np.nan,index=[],columns=['schoolyearsement','branch','active_users','retained_users','retention']创建数据框)
。因此不能有前导/拖尾空格。能否提供一个可复制/复制粘贴的示例?能否打印数据框的列以验证其是否如预期的那样存在?