diff --git a/doc/source/whatsnew/v0.15.2.txt b/doc/source/whatsnew/v0.15.2.txt index a1a353980f7aa..596bb841f16c9 100644 --- a/doc/source/whatsnew/v0.15.2.txt +++ b/doc/source/whatsnew/v0.15.2.txt @@ -155,6 +155,7 @@ Bug Fixes - Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`) - Fix: The font size was only set on x axis if vertical or the y axis if horizontal. (:issue:`8765`) - Fixed division by 0 when reading big csv files in python 3 (:issue:`8621`) +- Fixed Multindex to_html index=False adds an extra column (:issue:`8452`) diff --git a/pandas/core/format.py b/pandas/core/format.py index dbfe78d93bdcd..a17c45b70c74b 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -959,6 +959,10 @@ def _column_header(): name = self.columns.names[lnum] row = [''] * (row_levels - 1) + ['' if name is None else com.pprint_thing(name)] + + if row == [""] and self.fmt.index is False: + row = [] + tags = {} j = len(row) for i, v in enumerate(values): diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index ba3daf1b52045..80f1733ab4be5 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -571,6 +571,47 @@ def test_to_html_escape_disabled(self): """ self.assertEqual(xp, rs) + def test_to_html_multiindex_index_false(self): + # issue 8452 + df = pd.DataFrame({ + 'a': range(2), + 'b': range(3, 5), + 'c': range(5, 7), + 'd': range(3, 5)} + ) + df.columns = pd.MultiIndex.from_product([['a', 'b'], ['c', 'd']]) + result = df.to_html(index=False) + expected = """\ +
a | +b | +||
---|---|---|---|
c | +d | +c | +d | +
0 | +3 | +5 | +3 | +
1 | +4 | +6 | +4 | +