pyspark.pandas.MultiIndex.to_frame

MultiIndex.to_frame(index: bool = True, name: Optional[List[Union[Any, Tuple[Any, …]]]] = None) → pyspark.pandas.frame.DataFrame[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
indexboolean, default True

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

namelist / sequence of strings, optional

The passed names should substitute index level names.

Returns
DataFramea DataFrame containing the original MultiIndex data.

See also

DataFrame

Examples

>>> tuples = [(1, 'red'), (1, 'blue'),
...           (2, 'red'), (2, 'blue')]
>>> idx = ps.MultiIndex.from_tuples(tuples, names=('number', 'color'))
>>> idx  
MultiIndex([(1,  'red'),
            (1, 'blue'),
            (2,  'red'),
            (2, 'blue')],
           names=['number', 'color'])
>>> idx.to_frame()  
              number color
number color
1      red         1   red
       blue        1  blue
2      red         2   red
       blue        2  blue

By default, the original Index is reused. To enforce a new Index:

>>> idx.to_frame(index=False)
   number color
0       1   red
1       1  blue
2       2   red
3       2  blue

To override the name of the resulting column, specify name:

>>> idx.to_frame(name=['n', 'c'])  
              n     c
number color
1      red    1   red
       blue   1  blue
2      red    2   red
       blue   2  blue