davidjbeiler
davidjbeiler

Reputation: 133

How can i graph this as a stacked bar chart?

I have this code below, it produces my data frame exactly how i want it, but i can't seem to graph it via a grouped bar chart,

I'd like to have the department on the X axis and on the Y axis have completed with the remaining information on top

enter image description here

import pandas as pd
import matplotlib

data = pd.read_excel('audit.xls', skiprows=2, index_col = 'Employee Department')
data.rename(columns = {'Curriculum Name':'Curriculum','Organization Employee Number':'Employee_Number', 'Employee Name': 'Employee','Employee Email':'Email', 'Employee Status':'Status', 'Date Assigned':'Assigned','Completion Date':'Completed'}, inplace=True)
data.drop(['Employee_Number', 'Employee','Assigned', 'Status', 'Manager Name', 'Manager Email', 'Completion Status','Unnamed: 1', 'Unnamed: 5', 'Unnamed: 6'], axis=1, inplace=True)

new_data = data.query('Curriculum ==["CARB Security Training","OIS Support Training","Legal EO Training"]')
new_data2 = new_data.groupby('Employee Department').count().eval('Remaining = Email - Completed', inplace=False)

new_data2

I assume i need to convert it to a pivot table somehow since that's how it is in excel

Upvotes: 0

Views: 69

Answers (1)

Longwen Ou
Longwen Ou

Reputation: 879

Have you tried something like this: new_data2[['Completed','Remaining']].plot.bar(stacked=True)

The following example works for me:

df = pd.DataFrame(np.arange(1,10).reshape(3,3), columns=['Email', 'Completed', 'Remaining'], index=['A', 'B', 'C'])
df[['Completed', 'Remaining']].plot.bar(stacked=True)

enter image description here

Upvotes: 1

Related Questions