Skip to content

Commit 33f330e

Browse files
committed
Add example
1 parent f2ec344 commit 33f330e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

pandas/core/indexes/multi.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,13 +1798,26 @@ def to_flat_index(self):
17981798
17991799
Examples
18001800
--------
1801+
1802+
To convert a MultiIndex to an Index:
1803+
18011804
>>> index = pd.MultiIndex.from_product(
18021805
... [['foo', 'bar'], ['baz', 'qux']],
18031806
... names=['a', 'b'])
1804-
>>> index.to_flat_index()
1807+
>>> index = index.to_flat_index()
1808+
>>> index
18051809
Index([('foo', 'baz'), ('foo', 'qux'),
18061810
('bar', 'baz'), ('bar', 'qux')],
18071811
dtype='object')
1812+
1813+
To convert an Index back to a MultiIndex:
1814+
1815+
>>> pd.MultiIndex.from_tuples(index, names=['a', 'b'])
1816+
MultiIndex([('foo', 'baz'),
1817+
('foo', 'qux'),
1818+
('bar', 'baz'),
1819+
('bar', 'qux')],
1820+
names=['a', 'b'])
18081821
"""
18091822
return Index(self._values, tupleize_cols=False)
18101823

0 commit comments

Comments
 (0)