初学者Python:Panda Groupby函数(汇总列)

时间:2020-09-14 23:59:00

标签: python pandas dataframe group-by

我试图使用panda groupby / aggregate函数显示按ManagerID和JobTitle分组的总休假时间(VacationHours和SickLeaveHours的组合)。我不确定如何显示汇总VacationHours + SickLeaveHours的总列。

excel = pd.read_excel('Employees.xls','Sheet0',index_col=None,na_values=['NA'])
excel.groupby(['ManagerID','JobTitle']).agg(({'VacationHours': np.sum,'SickLeaveHours': 
np.sum}))

Below is the current output that shows the correct grouping but a column for vacationHours & SickLeaveHours where I want the output to be one total column

2 个答案:

答案 0 :(得分:0)

添加总数

out = excel.groupby(['ManagerID','JobTitle']).agg(({'VacationHours':np.sum,
                                                    'SickLeaveHours':np.sum}))
out['total'] = out.sum(axis=1)

答案 1 :(得分:0)

尝试使用*(mData + sampleIndex + i) = *(buffer + i);

transform