diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 90324044c237c..63f59794d363a 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -1163,6 +1163,9 @@ def _normalize( elif margins is True: # keep index and column of pivoted table + if table.empty: + raise ValueError("Can't get margins since the result dataframe is empty") + table_index = table.index table_columns = table.columns last_ind_or_col = table.iloc[-1, :].name