我有以下代码:
array = pd.DataFrame(np.object, index=[], columns=[])
array = array.append({'Project': b.name, 'Summary': card.name, \
'Key': card.id, 'Assignee': m, 'Points': s}, \
ignore_index=True)
array1 = pd.read_excel('ProjectCostUCP.xlsx', 'Лист1')
result = array[['Project', 'Assignee', 'Points']].groupby(['Project',
'Assignee']).sum(axis=1)
print(result.columns.tolist()) #HERE IS ONLY 'Points' BUT MUST BE 'Project'
AND 'Asignee' ALSO !!!
result = result.merge(array1, on=['Project'])
result['Value'] = result.Points * result.Price
writer = pd.ExcelWriter('third.xlsx')
array.to_excel(writer, "June")
result.to_excel(writer, "June_agregation")
writer.save()
但是,我从此代码中得到了KeyError,因为在结果数组中没有“ Project”列,并且merge()
函数不起作用。
在grouby()
函数调用之后,只有一列-“点”。
如何解决此问题?
答案 0 :(得分:0)
回答问题的正确方法-使用reset_index()函数。使用grouby()函数后,我们需要将MultiIndex重置为index。在代码中它将是:
result.reset_index().merge(array1, on=['Project'])