如何将其绘制为堆积条形图?

时间:2017-10-24 19:59:13

标签: python pandas matplotlib

我在下面有这个代码,它完全按照我想要的方式生成我的数据框,但我似乎无法通过分组条形图来绘制它,

我希望X轴和Y轴上的部门已经完成,其余信息位于顶部

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

我认为我需要以某种方式将其转换为数据透视表,因为它是如何在excel中

1 个答案:

答案 0 :(得分:1)

你有没有试过这样的事情:new_data2[['Completed','Remaining']].plot.bar(stacked=True)

以下示例适用于我:

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

相关问题