I'm new to Python, and working on this project looking at deals won by an IT company. For the data below I'd like to create a stacked bar chart that shows only the top 5 'VP-Manager' values per 'Client Category' i.e. the top 5 'VP-Manager' who have won the most deals per 'Client Category'.
[Data Sample][1]: https://i.stack.imgur.com/f9sdG.png
I took guidance from @Jon Clements response on the below question: Create a stacked bar chart of the N largest columns per row in a dataframe
This was my code:
top5 = (
# bring the client category back as a column to use as a grouping var
dataw.reset_index()
# make a long DF of client category/column/name value
.melt(id_vars='Client Category')
# order DF by highest values first
.sort_values('VP-Manager', ascending=False)
# group by the index and take the first 5 rows of each
.groupby('Client Category')
.head(5)
# pivot back so we've got an X & Y to chart...
.pivot('Client Category', 'VP-Manager')
# drop the value level as we don't need that
.droplevel(level=0, axis=1)
)
top5.plot.bar(stacked=True)
But I keep getting an error KeyError: 'VP-Manager'
Tried various things (and a couple other methods) but can't seem to fix this.
Please can someone help? Thank you!
Update: I'm looking for a chart similar to this - [Desired Output][2]: https://i.stack.imgur.com/yb6Xl.jpg