[英]KeyError when using for loop on dataframe to plot histograms
I have a dataframe similar to: 我有一个类似于的数据框:
df = pd.DataFrame({'Date': ['2016-01-05', '2016-01-05', '2016-01-05', '2016-01-05', '2016-01-08', '2016-01-08', '2016-02-01'], 'Count': [1, 2, 2, 3, 2, 0, 2]})
and I am trying to plot a histogram of Count
for each unique Date
我正在尝试为每个唯一的Date
绘制Count
的直方图
I've tried: 我试过了:
for date in df.Date.unique():
plt.hist([df[df.Date == '%s' %(date)]['Count']])
plt.title('%s' %(date))
which results in 导致
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-17-971a1cf07250> in <module>()
1 for date in df.Date.unique():
----> 2 plt.hist([df[df.Date == '%s' %(date)]['Count']])
3 plt.title('%s' %(date))
c:~\anaconda3\lib\site-packages\matplotlib\pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
2963 histtype=histtype, align=align, orientation=orientation,
2964 rwidth=rwidth, log=log, color=color, label=label,
-> 2965 stacked=stacked, data=data, **kwargs)
2966 finally:
2967 ax.hold(washold)
c:~\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1816 warnings.warn(msg % (label_namer, func.__name__),
1817 RuntimeWarning, stacklevel=2)
-> 1818 return func(ax, *args, **kwargs)
1819 pre_doc = inner.__doc__
1820 if pre_doc is None:
c:~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5925
5926 # basic input validation
-> 5927 flat = np.ravel(x)
5928
5929 input_empty = len(flat) == 0
c:~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in ravel(a, order)
1482 return asarray(a).ravel(order=order)
1483 else:
-> 1484 return asanyarray(a).ravel(order=order)
1485
1486
c:~\anaconda3\lib\site-packages\numpy\core\numeric.py in asanyarray(a, dtype, order)
581
582 """
--> 583 return array(a, dtype, copy=False, order=order, subok=True)
584
585
c:~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
581 key = com._apply_if_callable(key, self)
582 try:
--> 583 result = self.index.get_value(self, key)
584
585 if not lib.isscalar(result):
c:~\anaconda3\lib\site-packages\pandas\indexes\base.py in get_value(self, series, key)
1978 try:
1979 return self._engine.get_value(s, k,
-> 1980 tz=getattr(series.dtype, 'tz', None))
1981 except KeyError as e1:
1982 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:3332)()
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:3035)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6610)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6554)()
KeyError: 0
But when I try to simply print it, there is no problem: 但是当我尝试简单地打印它时,没有问题:
for date in df.Date.unique():
print([df[df.Date == '%s' %(date)]['Count']])
[0 1
1 2
2 2
3 3
Name: Count, dtype: int64]
[4 2
5 0
Name: Count, dtype: int64]
[6 2
Name: Count, dtype: int64]
What is the issue with calling plt.hist
on my dataframe the way that I have it here? 以我在此处的方式在数据帧上调用plt.hist
有什么问题?
You're passing a list of dataframes, which is causing a problem here. 您正在传递数据帧列表,这在这里引起了问题。 You could deconstruct a groupby
object and plot each one separately. 您可以解构groupby
对象并分别绘制每个对象。
gps = df.groupby('Date').Count
_, axes = plt.subplots(nrows=gps.ngroups)
for (_, g), ax in zip(df.groupby('Date').Count, axes):
g.plot.hist(ax=ax)
plt.show()
Take a look at the Visualisation docs if you need more sugar in your graph. 如果需要在图形中添加更多糖,请查看可视化文档。
Essentially you have two square brackets too much in your code. 本质上,您的代码中两个方括号太多了。
plt.hist([series]) # <- wrong
plt.hist(series) # <- correct
In the first case matplotlib would try to plot a histogram of a list of one element, which is non-numeric. 在第一种情况下,matplotlib会尝试绘制一个非数字元素列表的直方图。 That won't work. 那行不通。
Instead, removing the brackts and directly supplying the series, works fine 取而代之的是,删除花括号并直接提供该系列作品,效果很好
for date in df.Date.unique():
plt.hist(df[df.Date == '%s' %(date)]['Count'])
plt.title('%s' %(date))
Now this will create all histograms in the same plot. 现在,这将在同一图中创建所有直方图。 Not sure if this is desired. 不确定是否需要这样做。 If not, consider the incredibly short alternative: 如果没有,请考虑一个非常短的替代方案:
df.hist(by="Date")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.