gpt4 book ai didi

python - Pandas /pyplot直方图: can plot df but not subset

转载 作者:行者123 更新时间:2023-12-01 04:19:20 29 4
gpt4 key购买 nike

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、操作系统 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

最佳答案

您正在将 pandas.Series 传递给 matplotlib (df0.Zcoord)。然而,目前,matplotlib 对于是否喜欢输入 pandas 数据类型(而不是 numpy ndarray 的数据类型)有点犹豫不决。

在 matplotlib 源代码内部的某个时刻,直方图函数可能试图获取“我被要求处理的第一个项目”,并且它可能通过调用 input[ 0] 其中 input 是要求它咀嚼的任何内容。如果 inputnumpy.ndarray 那么一切都很好。但是,如果 inputpandas.Series 或(更糟糕)pandas.DataFrame,则表达式 input[0] 将具有非常不同的含义。在这种情况下,根据您提供给 plt.hist 的数据结构,在尝试对输入进行索引时很可能会出现 KeyError

在您的特定情况下,这可能对整个 df 工作正常,因为 df 可能有一个整数索引 ([0, 1, 2, ..., len(df)-1]),这是 DataFrame 中的默认行索引。但是,当您在 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)

我的猜测是一切都会好起来的。

关于python - Pandas /pyplot直方图: can plot df but not subset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900323/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com