尝试绘制DataFrame时,TypeError:不可哈希类型:'numpy.ndarray' [英] TypeError: unhashable type: 'numpy.ndarray' when trying to plot a DataFrame

查看:68
本文介绍了尝试绘制DataFrame时,TypeError:不可哈希类型:'numpy.ndarray'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,正在尝试从DataFrame创建绘图图.

I am new to Python and trying to create a plot graph from a DataFrame.

我使用了以下代码:

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    predictions.append(predicted)
    RegistrationRates.append(predicted)

data = {'Year':['2016','2017','2018','2019','2020','2021','2022','2023','2024','2025'], 'Registration Rate':predictions}
resultdf = pd.DataFrame(data)
print(resultdf)

plt.xlabel('Year')
plt.ylabel('Registration Rate')
plt.plot(resultdf)

看到以下输出:

0  2016   [50.68501406476124]
1  2017   [52.41297372600995]
2  2018    [54.0703599343735]
3  2019   [53.58327982434545]
4  2020  [55.647237533704754]
5  2021  [54.398197822219714]
6  2022   [55.06459335430334]
7  2023   [56.00171430250292]
8  2024   [55.70449088032122]
9  2025    [57.7127557392168]

但绘制的空白图形出现以下错误: TypeError:不可散列的类型:'numpy.ndarray'

but blank graph is plotted with following error: TypeError: unhashable type: 'numpy.ndarray'

下面提供了完整的堆栈跟踪:

Full stack-trace is provided below:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-1d843e3f6a23> in <module>
     58 plt.xlabel('Year')
     59 plt.ylabel('Registration Rate')
---> 60 plt.plot(resultdf)
     61 
     62 root.mainloop()

~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
   3356                       mplDeprecation)
   3357     try:
-> 3358         ret = ax.plot(*args, **kwargs)
   3359     finally:
   3360         ax._hold = washold

~\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
   1525         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1526 
-> 1527         for line in self._get_lines(*args, **kwargs):
   1528             self.add_line(line)
   1529             lines.append(line)

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
    404                 this += args[0],
    405                 args = args[1:]
--> 406             for seg in self._plot_args(this, kwargs):
    407                 yield seg
    408 

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    381             x, y = index_of(tup[-1])
    382 
--> 383         x, y = self._xy_from_xy(x, y)
    384 
    385         if self.command == 'plot':

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
    214         if self.axes.xaxis is not None and self.axes.yaxis is not None:
    215             bx = self.axes.xaxis.update_units(x)
--> 216             by = self.axes.yaxis.update_units(y)
    217 
    218             if self.command != 'plot':

~\Anaconda3\lib\site-packages\matplotlib\axis.py in update_units(self, data)
   1467         neednew = self.converter != converter
   1468         self.converter = converter
-> 1469         default = self.converter.default_units(data, self)
   1470         if default is not None and self.units is None:
   1471             self.set_units(default)

~\Anaconda3\lib\site-packages\matplotlib\category.py in default_units(data, axis)
    113         # default_units->axis_info->convert
    114         if axis.units is None:
--> 115             axis.set_units(UnitData(data))
    116         else:
    117             axis.units.update(data)

~\Anaconda3\lib\site-packages\matplotlib\category.py in __init__(self, data)
    180         self._counter = itertools.count(start=0)
    181         if data is not None:
--> 182             self.update(data)
    183 
    184     def update(self, data):

~\Anaconda3\lib\site-packages\matplotlib\category.py in update(self, data)
    197         data = np.atleast_1d(np.array(data, dtype=object))
    198 
--> 199         for val in OrderedDict.fromkeys(data):
    200             if not isinstance(val, VALID_TYPES):
    201                 raise TypeError("{val!r} is not a string".format(val=val))

TypeError: unhashable type: 'numpy.ndarray'

推荐答案

如果您检查列的类型 'Registration Rate',您将看到它的类型是 numpy.ndarray 如错误所示.

If you check the type of your column 'Registration Rate', you will see that it's type of numpy.ndarray as shown in the error.

type(resultdf['Registration Rate'][0])

那么,也许可以修改您的 predictions 创建以使其成为单个元素?

So, maybe modify your predictions creation to make it a single element?

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    # predicted is a numpy.ndarray, len = 1
    p = predicted[0]
    predictions.append(p)

然后运行您的代码.

这篇关于尝试绘制DataFrame时,TypeError:不可哈希类型:'numpy.ndarray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆