pandas 如何使用标称值在熊猫中绘制直方图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14248706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I plot a histogram in pandas using nominal values?
提问by Tim Stewart
Given:
鉴于:
ser = Series(['one', 'two', 'three', 'two', 'two'])
How do I plot a basic histogram of these values?
如何绘制这些值的基本直方图?
Here is an ASCII version of what I'd want to see in matplotlib:
这是我想在 matplotlib 中看到的 ASCII 版本:
X
X X X
-------------
one two three
I'm tired of seeing:
我看腻了:
TypeError: cannot concatenate 'str' and 'float' objects
回答by Andy Hayden
You could use the value_countsmethod:
您可以使用以下value_counts方法:
In [10]: ser.value_counts()
Out[10]:
two 3
one 1
three 1
and then plot this as a bar chart:
然后将其绘制为条形图:
ser.value_counts().plot(kind='bar')
Edit: I noticed that this doesn't keep the desired order. If you have a list/Series for this ordering (in this case ser[:3]will do) you can reindexbefore plotting:
编辑:我注意到这没有保持所需的顺序。如果您有此排序的列表/系列(在这种情况下ser[:3]会这样做),您可以reindex在绘图之前:
In [12]: ser.value_counts().reindex(ser[:3])
Out[12]:
one 1
two 3
three 1

