Skip to content

Commit a6af3ac

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

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pandas/core/reshape/pivot.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,10 @@ def _normalize(table, normalize, margins, margins_name='All'):
558558
elif margins is True:
559559

560560
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) # GH 17029 and possibly more
563+
else:
564+
index_margin = table.loc[margins_name, :].drop(margins_name)
562565
table = table.drop(margins_name, axis=1).drop(margins_name)
563566
# to keep index and columns names
564567
table_index_names = table.index.names
@@ -582,8 +585,11 @@ def _normalize(table, normalize, margins, margins_name='All'):
582585
column_margin = column_margin / column_margin.sum()
583586
index_margin = index_margin / index_margin.sum()
584587
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 # GH 17024 ?
591+
else:
592+
table.loc[margins_name] = index_margin
587593

588594
table = table.fillna(0)
589595

0 commit comments

Comments
 (0)