Bar plot with groupby

主宰稳场 提交于 2020-06-10 02:33:08

问题


My categorical variable case_satus takes on four unique values. I have data from 2014 to 2016. I would like to plot the distribution of case_status grouped by year. I try to this using:

df.groupby('year').case_status.value_counts().plot.barh()

And I get the following plot:

What I would like to have is a nicer represenation. For example where I have one color for each year, and all the "DENIED" would stand next to each other.

I think it can be achieved since the groupby object is a multi-index, but I don't understand it well enough to create the plot I want.


The solution is:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh()

and results in


回答1:


I think you need add unstack for DataFrame:

df.groupby('year').case_status.value_counts().unstack().plot.barh()

Also is possible change level:

df.groupby('year').case_status.value_counts().unstack(0).plot.barh()


来源:https://stackoverflow.com/questions/48238305/bar-plot-with-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!