簡體   English   中英

熊貓/ Pyplot直方圖:可以繪制DF,但不能繪制子集

[英]Pandas / pyplot histogram: can plot df but not subset

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

您正在將pandas.Series傳遞給matplotlib( df0.Zcoord )。 但是,目前,matplotlib對於是否喜歡使用pandas數據類型(而不是numpy ndarrayndarray

在matplotlib源代碼的某些地方,直方圖函數可能正在嘗試獲取“我被要求處理的第一項”,並且它可能通過調用input[0] ,其中input是任意值它被要求繼續咀嚼。 如果input是一個numpy.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)

我的猜測是一切都會很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM