Python Forum
How to obtain values under All when the margins of pivot_table is set to True? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to obtain values under All when the margins of pivot_table is set to True? (/thread-24771.html)



How to obtain values under All when the margins of pivot_table is set to True? - new_to_python - Mar-03-2020

Hi, anybody knows how the values under All (both row and column) were calculated. I heard that by setting margins equals to True, the values under All are the group statistics. I added the values in the first column and divided by 6, I got 2.4975635 but privot_table gave 2.668874. Similarly, I calculated the average on the first row (2+2.222222)/2 = 2.111111 but pivot_table gave 2.166667.

In [66]: tips.pivot_table(['tip_pct', 'size'], index=['time', 'day'], columns='s
    ...: moker', margins=True)                                                  
Out[66]: 
                 size                       tip_pct                    
smoker             No       Yes       All        No       Yes       All
time   day                                                             
Dinner Fri   2.000000  2.222222  2.166667  0.139622  0.165347  0.158916
       Sat   2.555556  2.476190  2.517241  0.158048  0.147906  0.153152
       Sun   2.929825  2.578947  2.842105  0.160113  0.187250  0.166897
       Thur  2.000000       NaN  2.000000  0.159744       NaN  0.159744
Lunch  Fri   3.000000  1.833333  2.000000  0.187735  0.188937  0.188765
       Thur  2.500000  2.352941  2.459016  0.160311  0.163863  0.161301
All          2.668874  2.408602  2.569672  0.159328  0.163196  0.160803