@@ -558,7 +558,10 @@ def _normalize(table, normalize, margins, margins_name='All'):
558
558
elif margins is True :
559
559
560
560
column_margin = table .loc [:, margins_name ].drop (margins_name )
561
- index_margin = table .loc [margins_name , :].drop (margins_name )
561
+ if isinstance (table .index , MultiIndex ):
562
+ index_margin = table .loc [margins_name , :].iloc [0 ].drop (margins_name )
563
+ else :
564
+ index_margin = table .loc [margins_name , :].drop (margins_name )
562
565
table = table .drop (margins_name , axis = 1 ).drop (margins_name )
563
566
# to keep index and columns names
564
567
table_index_names = table .index .names
@@ -582,8 +585,11 @@ def _normalize(table, normalize, margins, margins_name='All'):
582
585
column_margin = column_margin / column_margin .sum ()
583
586
index_margin = index_margin / index_margin .sum ()
584
587
index_margin .loc [margins_name ] = 1
585
- table = concat ([table , column_margin ], axis = 1 )
586
- table = table .append (index_margin )
588
+ table [margins_name ] = column_margin
589
+ if isinstance (table .index , MultiIndex ):
590
+ table .loc [(margins_name , '' ), :] = index_margin
591
+ else :
592
+ table .loc [margins_name ] = index_margin
587
593
588
594
table = table .fillna (0 )
589
595
0 commit comments