Built with Tutify.io

    AIStudyLabs

    Pandas Rename Index Values of DataFrame

    Pandas Rename Index Values of DataFrame

    To rename index values of a Pandas DataFrame, you can use the rename() method or the index attribute. In this article, I will explain multiple ways of how to rename a single index value and multiple index values of the pandas DataFrame using functions like DataFrame.rename(), DataFrame.index property with examples. When you update multiple row index values, you would need to create a Dict with key-value pair and use it with rename() function.

    Related: pandas Rename Column of DataFrame & pandas rename Index Name

    Key Points –

    <a id="h-quick-examples-of-rename-index-values"></a>

    Quick Examples of Rename Index Values

    Following are quick examples of renaming index values of DataFrame.

    
    # Quick examples of rename index values
    
    # Assign Indexs to DataFrame.index
    df.index = ['index_1', 'index_2', 'index_3', 'index_4']
    
    # Using DataFrame.rename() to rename index values
    df2 = df.rename(index={'r3': 'Index_3'})
    
    # Rename multiple index value
    df2 = df.rename(index={'r1':'Index_1','r2':'Index_2'})
    
    # Changing the DataFrame inplace
    df.rename(index={'r3': 'Index_3','r4': 'Index_4'}, inplace=True)
    
    # Append a value to DataFrame index row Using lambda function
    df2 = df.rename(index=lambda Index: Index+'_Index')
    

    To run some examples of renaming index values of pandas DataFrame, let’s create a Pandas DataFrame.

    
    # Create DataFrame
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,30000],
        'Duration':['30days','40days','35days','50days'],
        'Discount':[1000,2300,1200,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration  Discount
    r1    Spark  20000   30days      1000
    r2  PySpark  25000   40days      2300
    r3   Python  22000   35days      1200
    r4   pandas  30000   50days      2000
    

    <a id="h-changing-the-row-index-using-dataframe-index-attribute"></a>

    Changing the Row Index Using DataFrame.index() Attribute

    In order to rename/change the values for all row index, create a list with new indexes and assign it to DataFrame.index attribute. Note that If your DataFrame is small and for a huge DataFrame this is not an idle solution.

    
    # Change the row indexes
    df.index = ['index_1', 'index_2', 'index_3', 'index_4']
    print(df)
    

    Yields below output.

    
    # Output:
             Courses    Fee Duration  Discount
    index_1    Spark  20000   30days      1000
    index_2  PySpark  25000   40days      2300
    index_3   Python  22000   35days      1200
    index_4   pandas  30000   50days      2000
    

    <a id="h-rename-specific-index-values"></a>

    Rename Specific Index Values

    pandas.DataFrame.rename() accepts a dictionary as a parameter for the index values you want to rename. Simply pass a dictionary with key-value pairs where the key is the existing index you wish to rename, and the value is your desired new index value. The below examples changes the index to r3 from Index_3.

    
    # Using DataFrame.rename() to rename index values
    df = pd.DataFrame(technologies,index=index_labels)
    df2 = df.rename(index={'r3': 'Index_3'})
    print(df2)
    

    Yields below output.

    
    # Output:
             Courses    Fee Duration  Discount
    r1         Spark  20000   30days      1000
    r2       PySpark  25000   40days      2300
    Index_3   Python  22000   35days      1200
    r4        pandas  30000   50days      2000
    

    <a id="h-rename-multiple-row-indexes"></a>

    Rename Multiple Row Indexes

    You can also use the same approach to rename multiple index values or all indexes of Pandas DataFrame.

    
    # Rename multiple index value
    df = pd.DataFrame(technologies,index=index_labels)
    df2 = df.rename(index={'r1':'Index_1','r2':'Index_2'})
    print(df2)
    

    Yields below output.

    
    # Output:
             Courses    Fee Duration  Discount
    Index_1    Spark  20000   30days      1000
    Index_2  PySpark  25000   40days      2300
    r3        Python  22000   35days      1200
    r4        pandas  30000   50days      2000
    

    <a id="h-change-the-dataframe-inplace"></a>

    Change the DataFrame Inplace

    DataFrame.rename() takes parameter inplace=True to change the DataFrame inplace. In this case, no new DataFrame is returned, and the return value is None.

    
    # Changing the DataFrame inplace
    df = pd.DataFrame(technologies,index=index_labels)
    df.rename(index={'r3': 'Index_3','r4': 'Index_4'}, inplace=True)
    print(df)
    

    Yields below output.

    
    # Output:
             Courses    Fee Duration  Discount
    r1         Spark  20000   30days      1000
    r2       PySpark  25000   40days      2300
    Index_3   Python  22000   35days      1200
    Index_4   pandas  30000   50days      2000
    

    <a id="h-append-a-value-to-dataframe-index"></a>

    Append a Value to DataFrame Index

    Another approach would be using the lambda function. The below example add’s ‘_Index’ to all index values. This approach helps you to update/rename all indexes

    
    # Append a value to DataFrame index row Using lambda function
    df2 = df.rename(index=lambda Index: Index+'_Index')
    print(df2)
    

    Yields below output.

    
    # Output:
              Courses    Fee Duration  Discount
    r1_Index    Spark  20000   30days      1000
    r2_Index  PySpark  25000   40days      2300
    r3_Index   Python  22000   35days      1200
    r4_Index   pandas  30000   50days      2000
    

    <a id="h-complete-example-for-rename-index-values"></a>

    Complete Example for Rename Index Values

    
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas"],
        'Fee' :[20000,25000,22000,30000],
        'Duration':['30days','40days','35days','50days'],
        'Discount':[1000,2300,1200,2000]
                  }
    index_labels=['r1','r2','r3','r4']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    
    # Change the row indexes
    df.index = ['index_1', 'index_2', 'index_3', 'index_4']
    print(df)
    
    # Using DataFrame.rename() to rename index values
    df2 = df.rename(index={'r3': 'Index_3'})
    print(df2)
    
    # Rename multiple index value
    df2 = df.rename(index={'r1':'Index_1','r2':'Index_2'})
    print(df2)
    
    # Changing the DataFrame inplace
    df.rename(index={'r3': 'Index_3','r4': 'Index_4'}, inplace=True)
    print(df)
    
    # Append a value to DataFrame index row Using lambda function
    df2 = df.rename(index=lambda Index: Index+'_Index')
    print(df2)
    

    <a id="h-faq-on-rename-index-values-of-pandas-dataframe"></a>

    FAQ on Rename Index Values of Pandas DataFrame

    Pandas provides the rename() method which can be applied directly to the DataFrame object. You can specify the new index values using a dictionary-like mapping or a function.

    Renaming index values using the rename() method in Pandas does not modify the original DataFrame. Instead, it returns a new DataFrame with the updated index values, leaving the original DataFrame unchanged.

    You can selectively rename index values within a DataFrame using the rename() method in Pandas. By specifying the indices you want to rename and their corresponding new values, you can effectively rename index values selectively without affecting the rest of the DataFrame's index.

    You can rename index values selectively within a DataFrame in Pandas. By using the rename() method and specifying the indices you want to rename along with their new values, you can effectively rename index values selectively without altering the entire index.

    The rename() method allows for renaming both row and column index values simultaneously by specifying the index and columns parameters accordingly.

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

    Conclusion

    In this article, you have learned to rename index values of pandas DataFrame using DataFrame.index, DataFrame.rename() and lambda functions with examples. Additionally, you covered how to rename both single and multi-level index values.

    Happy Learning !!

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

    Related Articles

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

    References