python-3.x - python的新手,我需要从输出中绘制一个条形图,以百分比表示

标签 python-3.x pandas

我使用下面的代码按百分比过滤掉 DataFrame 中的前 5 个项目

df = df['Country'].value_counts(normalize=True).mul(100).round(2).astype(str) +'%' 
df.head(5)

输出

United States     32.78%
India              8.79%
France             4.75%
United Kingdom     4.35%
Germany            3.96%

Name: Country, dtype: object

如何使用条形图绘制输出?

最佳答案

您需要将百分比恢复为数字数据类型:

df.str[:-1].astype(float).plot.bar()

输出:

enter image description here

关于python-3.x - python的新手,我需要从输出中绘制一个条形图,以百分比表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61331087/

相关文章:

python - Pandas 将行中上一个值和下一个值的平均值归咎于 Null

python-3.x - 定时器触发不会触发队列但手动录入会触发-Python

python-3.x - 在将 Keras Tuner 与 Tensorflow 2 结合使用时,出现错误 : division by zero

python - 如何只打印按字母顺序嵌套在列表、字典中的内部字典的键?

python - 将附加参数传递给 Pandas 自定义访问器

python - 无法调用python中的函数

python - 从 pandas 数据帧生成字典,其中多个列组合为键,其余列作为值?

python - 1个生产者,1个消费者,只有1条数据要通信,队列是不是有点大材小用了?

python - 每次迭代更新字典,而不是整体更新

html - Django 不使用 CSS 读取我的 HTML