How do you rename a column index in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: Pandas

    A column of a data frame can be changed using the position it is in known as its index. Just by the use of index a column can be renamed. This article discusses all such possible methods. 

    Approach:

    • Import required python library.
    • Create data
    • Provide index of the column to be renamed as argument to rename() function.

    Pandas rename() method is used to rename any index, column or row.

    Syntax: rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None)

    Parameters:

    • mapper, index and columns: Dictionary value, key refers to the old name and value refers to new name. Only one of these parameters can be used at once.
    • axis: int or string value, 0/’row’ for Rows and 1/’columns’ for Columns
    • copy: Copies underlying data if True.
    • inplace: Makes changes in original Data Frame if True.
    • level: Used to specify level in case data frame is having multiple level index.

    Return Type: Data frame with new names

    Given below are various implementation to achieve our required functionality:

    Example 1: Switching both column names with each other using column index.

    Python3

    import pandas as pd

    df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})

    df.columns.values[0] = "b"

    df.columns.values[1] = "a"

    display(df)

    Output:

    How do you rename a column index in python?

    Example 2: Using another method to rename the column with index.

    Python3

    import pandas as pd

    df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})

    su = df.rename(columns={df.columns[1]: 'new'})

    display(su)

    Output:

    How do you rename a column index in python?

    Example 3: Renaming two/more columns in a single command using an index number.

    Python3

    import pandas as pd

    df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [7, 8]})

    mapping = {df.columns[0]: 'new0', df.columns[1]: 'new1'}

    su = df.rename(columns=mapping)

    display(su)

    Output:

    How do you rename a column index in python?

    Example 4: Renaming column name with an index number of the CSV file.

    File in use: Data1.csv

    Link: Clickhere

    Python3

    import pandas as pd

    df1 = pd.read_csv("data1.csv")

    df1.columns.values[2] = "city"

    display(df1)

    Output:

    How do you rename a column index in python?


    How do I rename a column in Python?

    You can use one of the following three methods to rename columns in a pandas DataFrame:.
    Method 1: Rename Specific Columns df. rename(columns = {'old_col1':'new_col1', 'old_col2':'new_col2'}, inplace = True).
    Method 2: Rename All Columns df. ... .
    Method 3: Replace Specific Characters in Columns df..

    How do you change the name of an index?

    Click the plus sign to expand the table on which you want to rename an index. Click the plus sign to expand the Indexes folder. Right-click the index you want to rename and select Rename. Type the index's new name and press Enter.

    How do I set an index column name?

    To set a column as index for a DataFrame, use DataFrame. set_index() function, with the column name passed as argument. You can also setup MultiIndex with multiple columns in the index. In this case, pass the array of column names required for index, to set_index() method.

    How do I rename a column name in pandas?

    One way of renaming the columns in a Pandas Dataframe is by using the rename() function.