【发布时间】:2016-02-27 06:54:15
【问题描述】:
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 吐出 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
【问题讨论】:
标签: python python-2.7 pandas matplotlib ipython-notebook