df.groupby() giving me wrong total calculations. pandas. numpy

Viewed 46

So I was just checking the final results after grouping, but the column sums are not matching. Here's my code (the last logical statement is failing even though the sums should be the same):

dfM = pd.concat([df1,df2])
dfM_V = sum(dfM['SumOfPCS'])

A = ['SOLDTO', 'PICKUP', 'ORIZIP3','ORIGINFACILITYCODE', 'PRODUCT_ID','ACTUALRECIPIENTCOUNTRY', 'LB_BRK','COUNTRY', 'MANIFESTEDDSPPRODUCT']
V = ['SumOfPCS', 'SumOfLBS']

dfM2 = dfM.groupby(A).agg([np.sum])[V]
dfM2 = dfM2.reset_index()
dfM2.columns = dfM2.columns.get_level_values(0)

dfM2_V = sum(dfM2['SumOfPCS']

print(dfM2_V == dfM_V)

By the way, A + V = list(dfM.columns) and there no empty rows nor cells in the dataset. (When I do the exact same grouping on MS Access, the logical condition tested at the end is met, so there's nothing inherently wrong with the dataset.)

0 Answers
Related