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

2024-06-28 11:12:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,它精确地生成了我想要的数据帧,但我似乎无法通过分组条形图来绘制它

我希望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中就是这样


Tags: nameimportnumbernewdataemailstatustraining
1条回答
网友
1楼 · 发布于 2024-06-28 11:12:26

你试过这样的方法吗: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

相关问题 更多 >