MCPcopy
hub / github.com/pandas-dev/pandas / to_frame

Method to_frame

pandas/core/indexes/base.py:1712–1774  ·  view source on GitHub ↗

Create a DataFrame with a column containing the Index. Parameters ---------- index : bool, default True Set the index of the returned DataFrame as the original Index. name : object, defaults to index.name The passed name should subst

(
        self, index: bool = True, name: Hashable = lib.no_default
    )

Source from the content-addressed store, hash-verified

1710 return Series(self._values.copy(), index=index, name=name)
1711
1712 def to_frame(
1713 self, index: bool = True, name: Hashable = lib.no_default
1714 ) -> DataFrame:
1715 """
1716 Create a DataFrame with a column containing the Index.
1717
1718 Parameters
1719 ----------
1720 index : bool, default True
1721 Set the index of the returned DataFrame as the original Index.
1722
1723 name : object, defaults to index.name
1724 The passed name should substitute for the index name (if it has
1725 one).
1726
1727 Returns
1728 -------
1729 DataFrame
1730 DataFrame containing the original Index data.
1731
1732 See Also
1733 --------
1734 Index.to_series : Convert an Index to a Series.
1735 Series.to_frame : Convert Series to DataFrame.
1736
1737 Examples
1738 --------
1739 >>> idx = pd.Index(["Ant", "Bear", "Cow"], name="animal")
1740 >>> idx.to_frame()
1741 animal
1742 animal
1743 Ant Ant
1744 Bear Bear
1745 Cow Cow
1746
1747 By default, the original Index is reused. To enforce a new Index:
1748
1749 >>> idx.to_frame(index=False)
1750 animal
1751 0 Ant
1752 1 Bear
1753 2 Cow
1754
1755 To override the name of the resulting column, specify `name`:
1756
1757 >>> idx.to_frame(index=False, name="zoo")
1758 zoo
1759 0 Ant
1760 1 Bear
1761 2 Cow
1762 """
1763 from pandas import DataFrame
1764
1765 if name is lib.no_default:
1766 result_name = self._get_level_names()
1767 else:
1768 result_name = Index([name]) # type: ignore[assignment]
1769 result = DataFrame(self, copy=False)

Callers 15

test_index_to_frameFunction · 0.95
left_error_msgMethod · 0.45
right_error_msgMethod · 0.45
_validate_operandFunction · 0.45
_numba_agg_generalMethod · 0.45
_transform_with_numbaMethod · 0.45

Calls 3

_get_level_namesMethod · 0.95
DataFrameClass · 0.90
IndexClass · 0.85

Tested by 3

test_index_to_frameFunction · 0.76