pandas 熊猫图值以降序方式计算条形图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49059956/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:15:09  来源:igfitidea点击:

pandas plot value counts barplot in descending manner

pythonpandasmatplotlibseaborn

提问by jxn

I have a dataframe where i am trying to count the occurrence of each value. I plot it as horizontal bar but cant get it to be sorted.

我有一个数据框,我试图在其中计算每个值的出现次数。我将其绘制为水平条,但无法对其进行排序。

df = pd.DataFrame(['A','A','A','B','B','C'],columns = ['letters'])

df.value_counts()

A 3
B 2
C 1

enter image description here

在此处输入图片说明

How can i get it sorted in a descending manner?

我怎样才能以降序方式对其进行排序?

回答by A.Gharbi

You can do it by changing your plotting line like this

你可以通过像这样改变你的绘图线来做到这一点

df.letters.value_counts().sort_values().plot(kind = 'barh')

回答by Peter Leimbigler

This might count as a bit of a hack, but try this:

这可能算作有点黑客,但试试这个:

df.letters.value_counts().sort_index(ascending=False).plot(kind='barh')