Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 熊猫:从字符串计数创建直方图_Pandas - Fatal编程技术网

Pandas 熊猫:从字符串计数创建直方图

Pandas 熊猫:从字符串计数创建直方图,pandas,Pandas,我需要从包含值“Low”、“Middle”或“High”的dataframe列创建一个直方图。当我尝试执行通常的df.column.hist()时,我得到以下错误 ex3.Severity.value_counts() Out[85]: Low 230 Medium 21 High 16 dtype: int64 ex3.Severity.hist() TypeError Traceback

我需要从包含值“Low”、“Middle”或“High”的dataframe列创建一个直方图。当我尝试执行通常的df.column.hist()时,我得到以下错误

ex3.Severity.value_counts()
Out[85]: 
Low       230
Medium     21
High       16
dtype: int64

ex3.Severity.hist()


TypeError                                 Traceback (most recent call last)
<ipython-input-86-7c7023aec2e2> in <module>()
----> 1 ex3.Severity.hist()

C:\Users\C06025A\Anaconda\lib\site-packages\pandas\tools\plotting.py in hist_series(self, by, ax, grid, xlabelsize, xrot, ylabelsize, yrot, figsize, bins, **kwds)
2570         values = self.dropna().values
2571 
->2572         ax.hist(values, bins=bins, **kwds)
2573         ax.grid(grid)
2574         axes = np.array([ax])

C:\Users\C06025A\Anaconda\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)
5620             for xi in x:
5621                 if len(xi) > 0:
->5622                     xmin = min(xmin, xi.min())
5623                     xmax = max(xmax, xi.max())
5624             bin_range = (xmin, xmax)

TypeError: unorderable types: str() < float()
ex3.Severity.value\u counts()
出[85]:
低230
中等21
高16
数据类型:int64
ex3.Severity.hist()
TypeError回溯(最近一次调用上次)
在()
---->1 ex3.Severity.hist()
C:\Users\C06025A\Anaconda\lib\site packages\pandas\tools\plotting.py历史系列(self、by、ax、grid、xlabelsize、xrot、ylabelsize、yrot、figsize、bin、**kwds)
2570值=self.dropna()值
2571
->2572 ax.hist(值,箱=箱,**千瓦时)
2573轴栅(栅)
2574轴=np.阵列([ax])
C:\Users\C06025A\Anaconda\lib\site packages\matplotlib\axes\\u axes.py(self、x、bins、range、normed、weights、累计、底部、histtype、align、orientation、rwidth、log、color、label、stacked、**kwargs)
5620席在X:
5621如果len(xi)>0:
> 5622 xmin=min(xmin,席.min)
5623 xmax=max(xmax,席max())
5624 bin_范围=(xmin,xmax)
TypeError:无序类型:str()
这是一个matplotlib问题,无法将字符串排序在一起,但是您可以通过标记x记号来实现所需的结果:

# emulate your ex3.Severity.value_counts()
data = {'Low': 2, 'Medium': 4, 'High': 5}
df = pd.Series(data)

plt.bar(range(len(df)), df.values, align='center')
plt.xticks(range(len(df)), df.index.values, size='small')
plt.show()

您假设,由于您的数据由字符串组成,因此对此调用
plot()
将自动执行
值计数()
,但情况并非如此,因此出现错误,因此您只需执行以下操作:

ex3.Severity.value_counts().hist()
这才是你真正想要的

当您这样做时:

ex3.Severity.value_counts().hist()
它以错误的方式获取轴,即尝试将y轴(计数)划分为多个存储箱,然后绘制每个存储箱中字符串标签的数量。

只是一个更新的答案(因为出现了很多)。Pandas有一个很好的模块,用于以多种方式设置数据帧的样式,例如上面提到的案例

ex3.Severity.value\u counts().to\u frame().style.bar()

…将使用内置条形图打印数据框(如迷你图,使用excel术语)。非常适合在jupyter笔记本上快速分析


请参见

我的猜测是因为您的数据只是您想要的字符串
ex3.Severity.value_counts().hist()
?谢谢,你说得对。我假设他的功能是内置于字符串中的,我不需要调用value\u counts,我应该发布作为答案吗?是的,你是对的。奇怪的是,这对有一列数字但有一列文本的熊猫来说效果很好,需要一些完全不同的东西。在Pandas v0.19.2中适用于我
ex3.Severity.value_counts().hist()