- API reference
- Index objects
- pandas.Index...
pandas.Index.to_frame#
- Index.to_frame(index=True,name=<no_default>)[source]#
Create a DataFrame with a column containing the Index.
- Parameters:
- indexbool, default True
Set the index of the returned DataFrame as the original Index.
- nameobject, defaults to index.name
The passed name should substitute for the index name (if it hasone).
- Returns:
- DataFrame
DataFrame containing the original Index data.
See also
Index.to_series
Convert an Index to a Series.
Series.to_frame
Convert Series to DataFrame.
Examples
>>>idx=pd.Index(['Ant','Bear','Cow'],name='animal')>>>idx.to_frame() animalanimalAnt AntBear BearCow Cow
By default, the original Index is reused. To enforce a new Index:
>>>idx.to_frame(index=False) animal0 Ant1 Bear2 Cow
To override the name of the resulting column, specifyname:
>>>idx.to_frame(index=False,name='zoo') zoo0 Ant1 Bear2 Cow
On this page