bigframes.pandas.DataFrame.reset_index#

DataFrame.reset_index(level: blocks.LevelsType = None, drop: bool = False, inplace: Literal[False] = False, col_level: int | str = 0, col_fill: Hashable = '', allow_duplicates: bool | None = None, names: None | Hashable | Sequence[Hashable] = None) DataFrame[source]#
DataFrame.reset_index(level: blocks.LevelsType = None, drop: bool = False, inplace: Literal[True] = False, col_level: int | str = 0, col_fill: Hashable = '', allow_duplicates: bool | None = None, names: None | Hashable | Sequence[Hashable] = None) None
DataFrame.reset_index(level: blocks.LevelsType = None, drop: bool = False, inplace: bool = False, col_level: int | str = 0, col_fill: Hashable = '', allow_duplicates: bool | None = None, names: None | Hashable | Sequence[Hashable] = None) DataFrame | None

Reset the index.

Reset the index of the DataFrame, and use the default one instead.

Examples:

>>> df = bpd.DataFrame([('bird', 389.0),
...                     ('bird', 24.0),
...                     ('mammal', 80.5),
...                     ('mammal', np.nan)],
...                    index=['falcon', 'parrot', 'lion', 'monkey'],
...                    columns=('class', 'max_speed'))
>>> df
         class  max_speed
falcon    bird      389.0
parrot    bird       24.0
lion    mammal       80.5
monkey  mammal       <NA>

[4 rows x 2 columns]

When we reset the index, the old index is added as a column, and a new sequential index is used:

>>> df.reset_index()
    index   class  max_speed
0  falcon    bird      389.0
1  parrot    bird       24.0
2    lion  mammal       80.5
3  monkey  mammal       <NA>

[4 rows x 3 columns]

We can use the drop parameter to avoid the old index being added as a column:

>>> df.reset_index(drop=True)
    class  max_speed
0    bird      389.0
1    bird       24.0
2  mammal       80.5
3  mammal       <NA>

[4 rows x 2 columns]

You can also use reset_index with MultiIndex.

>>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'),
...                                    ('bird', 'parrot'),
...                                    ('mammal', 'lion'),
...                                    ('mammal', 'monkey')],
...                                   names=['class', 'name'])
>>> columns = ['speed', 'max']
>>> df = bpd.DataFrame([(389.0, 'fly'),
...                     (24.0, 'fly'),
...                     (80.5, 'run'),
...                     (np.nan, 'jump')],
...                    index=index,
...                    columns=columns)
>>> df
               speed   max
class  name
bird   falcon  389.0   fly
       parrot   24.0   fly
mammal lion     80.5   run
       monkey   <NA>  jump

[4 rows x 2 columns]
>>> df.reset_index()
    class    name  speed   max
0    bird  falcon  389.0   fly
1    bird  parrot   24.0   fly
2  mammal    lion   80.5   run
3  mammal  monkey   <NA>  jump

[4 rows x 4 columns]
>>> df.reset_index(drop=True)
   speed   max
0  389.0   fly
1   24.0   fly
2   80.5   run
3   <NA>  jump

[4 rows x 2 columns]
Parameters:
  • level (int, str, tuple, or list, default None) – Only remove the given levels from the index. Removes all levels by default.

  • drop (bool, default False) – Do not try to insert index into dataframe columns. This resets the index to the default integer index.

  • inplace (bool, default False) – Whether to modify the DataFrame rather than creating a new one.

  • col_level (int or str, default 0) – If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first level.

  • col_fill (object, default '') – If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated.

  • allow_duplicates (bool, optional, default None) – Allow duplicate column labels to be created.

  • names (str or 1-dimensional list, default None) – Using the given string, rename the DataFrame column which contains the index data. If the DataFrame has a MultiIndex, this has to be a list or tuple with length equal to the number of levels

Returns:

DataFrame with the new index.

Return type:

bigframes.pandas.DataFrame