pandas.MultiIndex.to_frame

MultiIndex.to_frame(index=True, name=None)[source]

Create a DataFrame with the levels of the MultiIndex as columns.

Column ordering is determined by the DataFrame constructor with data as a dict.

Parameters
index:bool, default True

Set the index of the returned DataFrame as the original MultiIndex.

name:list / sequence of str, optional

The passed names should substitute index level names.

Returns
DataFrame:a DataFrame containing the original MultiIndex data.

See also

DataFrame

Two-dimensional, size-mutable, potentially heterogeneous tabular data.

Examples

>>> mi = pd.MultiIndex.from_arrays([['a', 'b'], ['c', 'd']])
>>> mi
MultiIndex([('a', 'c'),
            ('b', 'd')],
           )
>>> df = mi.to_frame()
>>> df
     0  1
a c  a  c
b d  b  d
>>> df = mi.to_frame(index=False)
>>> df
   0  1
0  a  c
1  b  d
>>> df = mi.to_frame(name=['x', 'y'])
>>> df
     x  y
a c  a  c
b d  b  d

© 2008–2021, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/1.3.4/reference/api/pandas.MultiIndex.to_frame.html