How to RENAME multiple Column Headers in a Pandas DataFrame in Python

January 17, 2019

Contact Seller

Guest Post Description

How to RENAME multiple Column Headers in a Pandas DataFrame in Python

def Snippet_103(): 
    print()
    print(format('How to rename multiple column headers in a Pandas DataFrame','*^82'))    
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    import pandas as pd
    # Set ipython's max row display
    pd.set_option('display.max_row', 1000)
    # Set iPython's max column width to 50
    pd.set_option('display.max_columns', 50)

    # Create an example dataframe
    data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
            'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08', 
                     '2012, 02, 08', '2012, 02, 08'], 
            'Score': [4, 24, 31, 2, 3]}
    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 
                                     'Maricopa', 'Yuma'])
    print(); print(df)
    # Rename Column Names
    df.columns = ['Leader', 'Time', 'Score']
    print(); print(df)
    # Rename Column Name Leader to Commander
    df.rename(columns={'Leader':'Commander'}, inplace=True)
    print(); print(df)
Snippet_103()

https://setscholars.com/DataScience/dd-product/how-to-rename-multiple-column-headers-in-a-pandas-dataframe-in-python/

Add a Review

You must be logged in to post a comment.