How to rename columns by index in Pandas

In this post, we will learn How to rename columns by index in Pandas DataFrame with code examples. We can rename columns by index using the Pandas dataframe rename() method or by using the index of the column, for this, we have to access the column by index and assigned a new name.

1. How to rename column by index in Pandas


In this example rename a column of pandas dataframe by index. Firstly we have accessed the column by index using dfobj.columns.values[2]. The Pandas DataFrame column index starts from 0 to a number of columns. We are renaming the column at index 2, with the new column name ‘ Addmission_Fee’, by assigning a new column name.

import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)

df.columns.values[2] = "Addmission_Fee"

print("Dataframe after rename Column:\n", dfobj.columns)

Output

Dataframe after rename Column:
 Index(['Stu_name', 'Marks', 'Addmission_Fee', 'Tution_Fee'], dtype='object')

2. How to rename multiple columns by index in Pandas


In this example, We will discuss, How to rename columns by index in Pandas DataFrame.To rename multiple columns of pandas dataframe. Firstly we have accessed the columns by index using dfobj.columns.values[0].The Pandas DataFrame column index starts from 0 to the number of columns. We are renaming the column at index 0 that is ‘Name’ with “Stu_name” and the column at index 2 with is ‘Addmission_Fee’ by assigning them new column names.

#python 3 Program How to rename columns by index in Pandas DataFrame

import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)

dfobj.columns.values[0] = "Stu_name"
df.columns.values[2] = "Addmission_Fee"

print("Dataframe after rename Column:\n", dfobj.columns)

Output

Dataframe after rename Column:
 Index(['Stu_name', 'Marks', 'Addmission_Fee', 'Tution_Fee'], dtype='object')

3. How to rename a column by index in Pandas using rename()


To rename a single column by index using rename() method of pandas DataFrame.We have passed a dictionary dfobj.columns[2]: ‘Addmission_Fee’} to the columns parameter of rename() method where the key represents the old column index and the value represents the new column name. The inplace=True indicates that we are making changes in the original Dataframe without creating a cop

  • Key: Dictionary key represents the old column index of Pandas dataframe
  • value: Dictionary value represents the new column names.
  • The inplace=True indicates that we are making changes in the original Dataframe without creating a copy.
import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)

dfobj.rename(columns={dfobj.columns[2]: 'Addmission_Fee'},inplace=True)


print(f"Dataframe after rename Column:\n{dfobj.columns}\n", )
print(dfobj)


Output

Dataframe after rename Column:
Index(['Name', 'Marks', 'Addmission_Fee', 'Tution_Fee'], dtype='object')

   Name  Marks  Addmission_Fee  Tution_Fee
0  Rama     97             100         400
1  Rama     97             100         400
2   Max    100             300         600
3  Rama     97             100         400

4. How to rename multiple columns by index in Pandas using rename()


To rename mutiple columns by index using rename() method of pandas DataFrame.We have passed a dictionary {dfobj.columns[2]: ‘Addmission_Fee’,dfobj.columns[0]: ‘Stu_Name’,dfobj.columns[1]: ‘Rank’},to the columns parameter of rename() method where the key represents the old column index and the value represented the new column name. The inplace=True indicates that we are making changes in the original Dataframe without creating a cop

import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)

dfobj.rename(columns={dfobj.columns[2]: 'Addmission_Fee',dfobj.columns[0]: 'Stu_Name',dfobj.columns[1]: 'Rank'},inplace=True)


print(f"Dataframe after rename Column:\n{dfobj.columns}\n", )
print(dfobj)

Output

Dataframe after rename Column:
Index(['Stu_Name', 'Rank', 'Addmission_Fee', 'Tution_Fee'], dtype='object')

  Stu_Name  Rank  Addmission_Fee  Tution_Fee
0     Rama    97             100         400
1     Rama    97             100         400
2      Max   100             300         600
3     Rama    97             100         400

5. How to rename columns by index in Pandas using slicing


In this example, we will understand how To rename multiple columns with list using the index. The dfobj.columns return an array of DataFrame columns, We have used slicing to ignore the First 2 columns and rename the Last two columns by assigning the list of new column names

  • Import pandas using “import pandas as pd”
  • Create a DataFrame of multiple columns from a dictionary.
  • The Dataframe. columns will return the array of DataFrame columns.
  • Finally, rename columns using slicing.

Rename multiple first last and middle column names of DataFrame

  • Rename Last N Columns (ignoring the first 2)
    • df.columns = df.columns[:N].tolist() + [‘new_col2’,’new_col3‘]
  • Rename First N columns (ignoring Last N )
    • df.columns = [‘new_col3′,’new_col4’] + df.columns[N:].tolist()
  • Rename Columns in the middle
    • df.columns = df.columns[:N].tolist() + [‘new_col2′,’new_col3’] + df.columns[Y:].tolist()
import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)
  
list_new_columns = [ 'Ranks','Enroll_Fee']


dfobj.columns = dfobj.columns[:2].tolist() + list_new_columns



print("Dataframe after rename Column:\n", dfobj.columns)

print('\n',dfobj)

Output

Dataframe after rename Column:
 Index(['Name', 'Marks', 'Ranks', 'Enroll_Fee'], dtype='object')

    Name  Marks  Ranks  Enroll_Fee
0  Rama     97    100         400
1  Rama     97    100         400
2   Max    100    300         600
3  Rama     97    100         400

6. How to rename columns by index in Pandas using index


To rename multiple columns with list in Pandas using an index.We have accessed the DataFrame columns using slicing and assigning a list of columns.The step we have followed as below

  • Import pandas using “import pandas as pd”
  • Create a DataFrame of multiple columns from a dictionary.
  • The Dataframe. column will return the array of DataFrame columns.
  • Finally, rename old column names of the dataframe with new column names using slicing.
import pandas as pd
  
     
data = {
    'Name': ['Rama', 'Rama', 'Max', 'Rama'],     
    'Marks':[97,97,100,97],    
    'Fee':[100,100,300,100],    
    'Tution_Fee':[400,400,600,400]
}
   
   
dfobj = pd.DataFrame(data)
  
list_new_columns = [ 'Stu_Name','Rank']


dfobj.columns.values[0:2] = list_new_columns



print("Dataframe after rename Column:\n", dfobj.columns)

print('\n',dfobj)

Output

sDataframe after rename Column:
 Index(['Stu_Name', 'Rank', 'Fee', 'Tution_Fee'], dtype='object')

   Stu_Name  Rank  Fee  Tution_Fee
0     Rama    97  100         400
1     Rama    97  100         400
2      Max   100  300         600
3     Rama    97  100         400

Summary

In this post, we have learned How to rename columns by index in Pandas with examples by using the column index and built-in rename() method of Pandas DataFrame both can be used to rename single or multiple columns in Pandas.