python3 pandas 数据求和,求平均处理

def test09():
    INPUTFILE1 = os.path.join(BASE_PATH, 'Students_Duplicates.xlsx')
    datas = ExcelUntil.readExcelFile(INPUTFILE1, headerRow=0, indexCol='ID')
    newdatas={}
    for sheetname,sheetdatas in datas.items():
        sheetdatas['Test_1']=sheetdatas['Test_1'].astype('int')
        sheetdatas['Test_2']=sheetdatas['Test_2'].astype('int')
        sheetdatas['Test_3']=sheetdatas['Test_3'].astype('int')
        dupe=sheetdatas.duplicated(subset='Name')
        dupe=dupe[dupe==True]
        sheetdatas.drop_duplicates(subset='Name',inplace=True,keep='last') 
        # sheetdatas['Total']=sheetdatas['Test_1']+sheetdatas['Test_2']+sheetdatas['Test_3']
        sheetdatas['Total']=sheetdatas[['Test_1','Test_2','Test_3']].sum(axis=1)
        sheetdatas['Averrage']=sheetdatas[['Test_1','Test_2','Test_3']].mean(axis=1)
        col_mean=sheetdatas[['Test_1','Test_2','Test_3','Total','Averrage']].mean()
        col_mean['Name']='Summary'
        sheetdatas=sheetdatas.append(col_mean,ignore_index=True)
        sheetdatas.index.names=['ID']
        pd.options.display.max_columns = 999
        print(sheetdatas)
        newdatas[sheetname]=sheetdatas.T
    OUTPUTFILE1=os.path.join(BASE_PATH,'result.xlsx')
    ExcelUntil.writeExcelFile(OUTPUTFILE1,newdatas,headerRow=1,indexFlag=True)

python3 pandas 数据求和,求平均处理_第1张图片

你可能感兴趣的:(Python)