Built with Tutify.io

    AIStudyLabs

    Pandas Series reset_index() Function

    Pandas Series reset_index() Function

    In Pandas, you can reset the index of a Series using the reset_index() method. This function resets the index to the default integer index and converts the original index to a new column in the DataFrame.

    In this article, I will explain the reset_index() function and using its syntax, parameters, and usage how we can reset the index of a DataFrame or Series. When applied to a Series, it returns a DataFrame with the original values of the Series as a new column, and a default sequential index.

    Key Points –

    <a id="h-pandas-series-reset-index-introduction"></a>

    Pandas Series reset_index() Introduction

    Following is the syntax of the pandas series reset_index() function.

    
    # Syntax of Series reset_index() function
    Series.reset_index(level=None, drop=False, name=None, inplace=False)
    

    <a id="h-parameters-of-the-nbsp-series-reset-index"></a>

    Parameters of the Series reset_index()

    Following are the parameters of the Series reset_index() function.

    <a id="h-return-value"></a>

    Return Value

    It returns the reset_index() function in Pandas when applied to a Series is a DataFrame. This DataFrame will have two columns: one for the original index (by default named “index”) and one for the values of the Series.

    <a id="h-create-pandas-series"></a>

    Create Pandas Series

    Pandas Series can be created in several ways by using Python lists & dictionaries, below example create a Series from a dictionary. To use Pandas first, you need to import using import pandas as pd.

    
    import pandas as pd
    
    # Create Pandas Series
    Courses = {'Spark': 20000, 'PySpark': 15000, 'Java': 10000}
    series = pd.Series(Courses)
    print("Original Series:\n",series)
    

    Yields below output.

    The reset_index() function is used to reset the index of a DataFrame or Series. When you manipulate a DataFrame or Series, it’s possible to end up with an index that doesn’t reflect the actual position of the data. The reset_index() function helps in such situations by moving the current index to a new column and resetting the index to the default integer index.

    
    # Reset the index
    result = series.reset_index()
    print("Reset Series:\n",result)
    

    In the above examples, the index of the original Series has been moved to a new column called index, and the default integer index has been applied to the Series. This example yields the below output.

    <a id="h-resetting-the-index-of-a-series-with-custom-column-names"></a>

    Resetting the Index of a Series with Custom Column Names

    To reset the index of a Pandas Series with custom column names, you can use the reset_index() function along with the name parameter to specify the name of the column containing the Series values.

    
    # Resetting the index with custom column names
    result = series.reset_index(name='value', drop=True)
    print("Reset Series with custom column names:\n",result)
    
    # Output:
    # Reset Series with custom column names:
    #      value
    # 0    20000
    # 1    15000
    # 2    10000
    # dtype: int64
    

    In the above example, the reset_index() function resets the index of the Series and renames the column containing the Series values to value using the name parameter. The drop=True parameter is used to drop the old index. As a result, the Series is converted into a DataFrame with a single column named value.

    <a id="h-resetting-the-index-of-a-series-and-keeping-the-old-index"></a>

    Resetting the Index of a Series and Keeping the Old Index

    Alternatively, to reset the index of a Pandas Series while keeping the old index as a column in the resulting DataFrame, you can set the drop parameter of the reset_index() function to False.

    
    # Resetting the index and keeping the old index
    result = series.reset_index(name='value', drop=False)
    print("Reset Series with old index as a column:\n",result)
    
    # Output:
    # Reset Series with old index as a column:
    #       index  value
    # 0    Spark  20000
    # 1  PySpark  15000
    # 2     Java  10000
    

    In the above example, the reset_index() function resets the index of the Series and retains the old index as a column named index in the resulting DataFrame. The drop=False parameter ensures that the old index is kept.

    <a id="h-resetting-the-index-of-a-series-and-preserving-the-original-index"></a>

    Resetting the Index of a Series and Preserving the Original Index

    To reset the index of a Pandas Series while preserving the original index, you can use reset_index() function without specifying any additional parameters. By default, reset_index() will move the original index to a new column in the resulting DataFrame.

    
    # Resetting the index and preserving the original index
    result = series.reset_index(drop=True)
    print("Reset Series with preserved original index:\n",result)
    
    # Output:
    # Reset Series with preserved original index:
    # 0    20000
    # 1    15000
    # 2    10000
    # dtype: int64
    

    In the above example, the reset_index() function resets the index of the Series and moves the original index to a new column in the resulting DataFrame. Setting drop=True ensures that the original index is not retained as a separate column in the DataFrame.

    <a id="h-resetting-the-index-of-a-series-and-handling-missing-values"></a>

    Resetting the Index of a Series and Handling Missing Values

    Similarly, to reset the index of a Pandas Series and handle missing values, you can first reset the index using reset_index(), and then handle the missing values as needed.

    
    import pandas as pd
    import numpy as np
    
    # Create Pandas Series
    Courses = {'Spark': 20000, 'PySpark': None, 'Java': 10000}
    series = pd.Series(Courses)
    
    # Resetting the index
    result = series.reset_index(drop=True)
    print("Reset Series:\n",result)
    
    # Output:
    # Reset Series:
    # 0    20000.0
    # 1        NaN
    # 2    10000.0
    # dtype: float64
    
    # Handling missing values
    result = result.dropna()
    print("Reset Series with missing values removed:\n",result)
    
    # Resetting the index and handling missing values
    result = series.reset_index(drop=True).dropna()
    print("Reset Series with missing values removed:\n",result)
    
    # Output:
    # Reset Series with missing values removed:
    # 0    20000.0
    # 2    10000.0
    # dtype: float64
    

    In the above example, the reset_index() function is used to reset the index of the Series, and the drop=True parameter is used to drop the original index. The resulting Series, result, contains missing values represented as NaN. Then, dropna() is used to remove the rows containing missing values, resulting in a Series without missing values.

    <a id="h-resetting-the-index-of-a-series-with-a-specified-fill-value-for-missing-values"></a>

    Resetting the Index of a Series with a Specified fill Value for Missing Values

    You can reset the index of a Pandas Series with a specified fill value for missing values, you can first reset the index using reset_index(), and then use the fillna() method to replace the missing values with the specified fill value.

    
    # Filling missing values with a specified fill value
    fill_value = -1
    result = result.fillna(fill_value)
    print("Reset Series with missing values filled:\n",result)
    
    # Output:
    # Reset Series with missing values filled:
    # 0    20000.0
    # 1       -1.0
    # 2    10000.0
    # dtype: float64
    

    In the above example, the reset_index() function is used to reset the index of the Series, and the drop=True parameter is used to drop the original index. The resulting Series, result, contains missing values represented as NaN. Then, fillna() is used to replace the missing values with the specified fill value -1, resulting in a Series with missing values filled with -1.

    <a id="h-frequently-asked-questions-on-nbsp-pandas-series-reset-index"></a>

    Frequently Asked Questions on Pandas Series reset_index()

    The reset_index() function in Pandas Series resets the index of the Series, converting it into a DataFrame. It moves the current index (if any) to a new column and resets the index to the default integer index.

    By setting the drop parameter to True (which is the default behavior), reset_index() drops the current index and returns a DataFrame with the default integer index.

    To preserve the original index while resetting the index of a Pandas Series, you can set the drop parameter of the reset_index() function to False. By default, the drop parameter is set to True, which removes the original index after resetting. Setting it to False retains the original index as a separate column in the resulting DataFrame.

    By default, the reset_index() function in Pandas returns a new object. It does not modify the original Series inplace. If you want to modify the original Series inplace, you can use the inplace=True parameter. Setting inplace=True will modify the original Series, and it will return None.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned the reset_index() function in pandas is used to reset the index of a DataFrame or Series. When you reset the index of a Series, it converts the index into a new column and generates a new default integer index.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference