使用pandas和matplotlib绘制分类数据 [英] Plotting categorical data with pandas and matplotlib

查看:123
本文介绍了使用pandas和matplotlib绘制分类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含分类数据的数据框:

I have a data frame with categorical data:

     colour  direction
1    red     up
2    blue    up
3    green   down
4    red     left
5    red     right
6    yellow  down
7    blue    down

我想根据类别生成一些图形,例如饼图和直方图.是否可以不创建虚拟数值变量?像

I want to generate some graphs, like pie charts and histograms based on the categories. Is it possible without creating dummy numeric variables? Something like

df.plot(kind='hist')

推荐答案

您只需在系列中使用value_counts:

df['colour'].value_counts().plot(kind='bar')

这篇关于使用pandas和matplotlib绘制分类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆