gpt4 book ai didi

pandas - 如何使用标称值绘制 Pandas 的直方图?

转载 作者:行者123 更新时间:2023-12-03 12:28:41 24 4
gpt4 key购买 nike

鉴于:

ser = Series(['one', 'two', 'three', 'two', 'two'])

如何绘制这些值的基本直方图?

这是我想在matplotlib中看到的ASCII版本:
     X
X X X
-------------
one two three

我讨厌看到:
TypeError: cannot concatenate 'str' and 'float' objects

最佳答案

您可以使用 value_counts 方法:

In [10]: ser.value_counts()
Out[10]:
two 3
one 1
three 1

然后 plot this as a bar chart:
ser.value_counts().plot(kind='bar')

编辑:我注意到这并不能保持所需的顺序。如果您有用于此排序的列表/系列(在这种情况下, ser[:3]可以),则可以在绘制之前 reindex :
In [12]: ser.value_counts().reindex(ser[:3])
Out[12]:
one 1
two 3
three 1

关于pandas - 如何使用标称值绘制 Pandas 的直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com