Skip to content

Commit 0d4f3e3

Browse files
committed
Hack to fix pandas-dev#15150
1 parent 9e7666d commit 0d4f3e3

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pandas/core/reshape/pivot.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,11 @@ def _normalize(table, normalize, margins, margins_name='All'):
556556
table = table.fillna(0)
557557

558558
elif margins is True:
559-
560559
column_margin = table.loc[:, margins_name].drop(margins_name)
561-
index_margin = table.loc[margins_name, :].drop(margins_name)
560+
if isinstance(table.index, MultiIndex):
561+
index_margin = table.loc[margins_name, :].iloc[0].drop(margins_name)
562+
else:
563+
index_margin = table.loc[margins_name, :].drop(margins_name)
562564
table = table.drop(margins_name, axis=1).drop(margins_name)
563565
# to keep index and columns names
564566
table_index_names = table.index.names
@@ -582,8 +584,11 @@ def _normalize(table, normalize, margins, margins_name='All'):
582584
column_margin = column_margin / column_margin.sum()
583585
index_margin = index_margin / index_margin.sum()
584586
index_margin.loc[margins_name] = 1
585-
table = concat([table, column_margin], axis=1)
586-
table = table.append(index_margin)
587+
table[margins_name] = column_margin
588+
if isinstance(table.index, MultiIndex):
589+
table.loc[(margins_name, ''), :] = index_margin
590+
else:
591+
table.loc[margins_name] = index_margin
587592

588593
table = table.fillna(0)
589594

0 commit comments

Comments
 (0)