Python Pandas/pyplot直方图:可以绘制df,但不能绘制子集
df是一个巨大的数据帧。我只需要Zcoord>1的子集Python Pandas/pyplot直方图:可以绘制df,但不能绘制子集,python,python-2.7,pandas,matplotlib,ipython-notebook,Python,Python 2.7,Pandas,Matplotlib,Ipython Notebook,df是一个巨大的数据帧。我只需要Zcoord>1的子集 df = pandas.DataFrame(first) df.columns = ['Xcoord', 'Ycoord', 'Zcoord', 'Angle'] df0 = df[df.Zcoord>1] 绘制df直方图的代码对df0不起作用 plot1 = plt.figure(1) plt.hist(df0.Zcoord, bins=100, normed=False) plt.show() Ipython吐出KeyErro
df = pandas.DataFrame(first)
df.columns = ['Xcoord', 'Ycoord', 'Zcoord', 'Angle']
df0 = df[df.Zcoord>1]
绘制df直方图的代码对df0不起作用
plot1 = plt.figure(1)
plt.hist(df0.Zcoord, bins=100, normed=False)
plt.show()
Ipython吐出KeyError:0
python 2.7.9 anaconda、ipython 2.2.0、OS 10.9.4
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-42-71643df3888f> in <module>()
1 plot1 = plt.figure(1)
----> 2 plt.hist(df0.Zcoord, bins=100, normed=False)
3
4 plt.show()
5 from matplotlib.backends.backend_pdf import PdfPages
/Users/Kit/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
2888 histtype=histtype, align=align, orientation=orientation,
2889 rwidth=rwidth, log=log, color=color, label=label,
-> 2890 stacked=stacked, **kwargs)
2891 draw_if_interactive()
2892 finally:
/Users/Kit/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5560 # Massage 'x' for processing.
5561 # NOTE: Be sure any changes here is also done below to 'weights'
-> 5562 if isinstance(x, np.ndarray) or not iterable(x[0]):
5563 # TODO: support masked arrays;
5564 x = np.asarray(x)
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
482 def __getitem__(self, key):
483 try:
--> 484 result = self.index.get_value(self, key)
485
486 if not np.isscalar(result):
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key)
1194
1195 try:
-> 1196 return self._engine.get_value(s, k)
1197 except KeyError as e1:
1198 if len(self) > 0 and self.inferred_type in ['integer','boolean']:
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:2993)()
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_value (pandas/index.c:2808)()
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3440)()
KeyError: 0
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在()
1图1=plt.图(1)
---->2 plt.hist(df0.Zcoord,仓位=100,规范=False)
3.
4 plt.show()
5从matplotlib.backends.backend\u pdf导入PdfPages
/hist中的用户/Kit/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc
2888 histtype=histtype,align=align,orientation=orientation,
2889 rwidth=rwidth,log=log,color=color,label=label,
->2890堆叠=堆叠,**千克)
2891 draw_if_interactive()
2892最后:
/Users/Kit/anaconda/lib/python2.7/site-packages/matplotlib/axes//u axes.pyc(self、x、bin、range、normed、weights、cumulative、bottom、histtype、align、orientation、rwidth、log、color、label、stacked、**kwargs)
5560#按摩“x”进行处理。
5561#注意:确保此处的任何更改也在下面对“权重”进行了更改
->5562如果存在(x,np.ndarray)或不可迭代(x[0]):
5563#待办事项:支持屏蔽阵列;
5564 x=np.asarray(x)
/Users/Kit/anaconda/lib/python2.7/site-packages/pandas/core/series.pyc in____getitem____(self,key)
482 def_uugetItem_uu(self,key):
483尝试:
-->484结果=self.index.get_值(self,key)
485
486如果不是np.isscalar(结果):
/获取值(self、series、key)中的Users/Kit/anaconda/lib/python2.7/site-packages/pandas/core/index.pyc
1194
1195尝试:
->1196返回自引擎。获取值(s,k)
1197除键错误为e1外:
1198如果len(self)>0且self.u键入['integer','boolean']:
/pandas.index.IndexEngine.get_值中的Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so(pandas/index.c:2993)()
/pandas.index.IndexEngine.get_值中的Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so(pandas/index.c:2808)()
/pandas.index.IndexEngine.get_loc(pandas/index.c:3440)()中的Users/Kit/anaconda/lib/python2.7/site-packages/pandas/index.so
关键错误:0
您正在将一个pandas.Series
传递给matplotlib(df0.Zcoord
)。然而,目前,matplotlib对于是否喜欢被输入数据类型(而不是numpyndarray
)有点犹豫不决
在matplotlib源代码内部的某个点上,直方图函数可能正试图获取“我被要求处理的第一项”,它可能通过调用input[0]
来实现这一点,其中input
是它被要求仔细研究的内容。如果input
是一个numpy.ndarray
那么一切都很好。但是,如果input
是pandas.Series
或(更糟糕的是)是pandas.DataFrame
,则表达式input[0]
将具有非常不同的含义。在这种情况下,根据您提供给plt.hist
的数据结构,在尝试索引到您的输入时,很可能会出现KeyError
在您的特定情况下,这可能在整个df
上运行良好,因为df
可能有一个整数索引([0,1,2,…,len(df)-1]
),这是数据帧中的默认行索引。但是,当您在df
中选择以生成df0
时,结果会以一个索引结束,该索引是df
索引的子集(可能会结束[3,6,9,12,…]
)。因此,在df
(其中索引包含0
)上一切都可以正常工作,但在df0
(具有讽刺意味的是,在该索引中给出了它的名称,0
)
快速修复…而不是
plt.hist(df0.Zcoord, bins=100, normed=False)
运行这个
plt.hist(df0.Zcoord.values, bins=100, normed=False)
我想一切都会好起来的。很高兴能帮上忙!如果你感兴趣,这是讨论这个问题的线索。如果您可以在原始帖子中包含更多的堆栈跟踪,这样其他人就可以准确地看到错误的来源,那将是非常棒的。