Prasun Kumar Khan
Prasun Kumar Khan

Reputation: 163

Pandas pivot_table function showing incorrect margin total

I have a base dataset (pd.DataFrame), on which I am trying to apply pivot_table function. When I use just one column as 'values', I get the correct margins total. However, when I use a list of two columns as 'values', the margin totals become incorrect. I am using the below codes [corresponding outputs are shown alongside]:

Code:

DetailedTable_GBP.pivot_table(index=['Level','Index','Currency'], values=['Daily','MTD'], aggfunc=sum, margins=True)

Output:

                              Daily     MTD
 Level   Index     Currency  
-----------------------------------------------
 Foo     Apples    GBP        18,385   -3,962
 Bar     Oranges   GBP        4,444    -79,282
 All                          22,830   -83,369 

Code:

DetailedTable_GBP.pivot_table(index=['Level','Index','Currency'], values=['MTD'], aggfunc=sum, margins=True)

Output:

                              MTD
 Level   Index     Currency   
------------------------------------
 Foo     Apples    GBP       -3,962
 Bar     Oranges   GBP      -79,282
 All                        -83,244 

What can I do to accomplish the correct margins total with multiple 'values'?

Edit 1. I figured out that I was getting different totals because there was a NaN in the 'Daily' column of my base data. Thus, the question is answered. Apologies for posting the question. I should have first checked the base data thoroughly.

Upvotes: 1

Views: 2131

Answers (2)

Prasun Kumar Khan
Prasun Kumar Khan

Reputation: 163

Because I had NaN values in my base dataset (specifically in 'Daily' column), applying fillna(0) to my base dataset resolved the issue of incorrect margins total.

Correct Code:

DetailedTable_GBP.fillna(0).pivot_table(index=['Level','Index','Currency'], values=['Daily','MTD'], aggfunc=sum, margins=True)

Upvotes: 1

Jakub
Jakub

Reputation: 549

DetailedTable_GBP.pivot_table(index=['Level', 'Index', 'Currency'], 
                             values=['Daily', 'MTD'], 
                            aggfunc={'Daily': sum,'MTD': sum}, 
                            margins=True)

Upvotes: 0

Related Questions