user8322222
user8322222

Reputation: 529

Stacked bar chart from grouped pandas series

I have a dataframe with 3 columns: ID, date, and priority:

ID  date   priority
1   2007   High
2   2007   Low
3   2010   Medium
4   2007   High

This is 12,000 rows in length. Every ID (person) is unique and has a date and priority assigned

what I want to do is plot a bar chart with Year on the x axis and the height being the total number of people (ID's) but it needs to be stacked by priority. So suppose I have 10 Ids in 2007 (5 from medium, 3 low, and 2 high) I need to plot 2007 with height 10 stacked accordingly.

This is what I am trying:

total = data.groupby(['date']).priority.value_counts()

This gives me the head of the pandas series:

date       priority
2000        High               6
            Medium             5
            Low                4
2001        High               6
            Medium             2
2002        High               1
            Medium             3
            Low                4
2003        High              10
            Medium             3

How do I plot date on x axis and the total of numbers on height broken down by H,M,L.

Upvotes: 0

Views: 93

Answers (1)

BENY
BENY

Reputation: 323376

You may check with crosstab

pd.crosstab(df.date,df.priority).plot(kin='bar')

Upvotes: 2

Related Questions