Articles

Change column names in Pandas DataFrame | rename multiple column names in pandas

Change column names in Pandas DataFrame | rename multiple column names in pandas


In this chapter of Pandas tutorial, we will learn about:

  • change column names in pandas dataframe using rename method,
  • rename multiple column names in pandas using rename method,
  • how to change all column names in pandas dataframe using columns method, 
  • change multiple column names in pandas using set_axis method
  • change column names in pandas using method str.replace (single, double or multiple)

In previous chapters, we have already explained about DataFrame and how to create one. Here, we will check one more example and then begin with change column names in pandas dataframe or rename column names in pandas dataframe and rename multiple column names in pandas using different functions.

 

What is DataFrame?

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Example:

In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign index labels using index parameter.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

#display the dataframe

print(data)

Output: Given below is the output dataframe

Now, we will work on this dataframe in upcoming explanation.


Method 1 : change column names in pandas Using rename()

The function rename() is used to change single or multiple columns at a time.

 

It will take columns parameter with a dictionary and take new column names as values and old column names as keys. So based on the key – it will set the new column names.

 

So this function can easily help rename column names in pandas dataframe.

Syntax:

dataframe.rename(columns={‘old_column’:’new_column’,….})

Where,

1.  dataframe is the input dataframe.

2.  old_column refers to the old column name

3.  new_column refers to the new column name

 

Example 1: change column names in pandas dataframe

In this example, we are going to change college_id column to college_ID

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

# change the single column name

data=data.rename(columns={'college_id':'college_ID'})

#display dataframe

print(data)

Output:Given below is output of rename column names in pandas dataframe from college_id to college_ID

         college_ID       college_name college_address  Total Staff
college1      c-001  vignan university          guntur         1200
college2      c-021               vvit          guntur         3422
college3      c-002           RVR - JC          guntur         5644
college4      c-004  Andhra University          guntur          670

Since we have already seen how to change column names in pandas dataframe lets see how to change all column names in pandas dataframe, or change multiple column names in pandas.


Example 2change multiple column names in pandas

In this example, we are going to rename multiple column names in pandas. So here we will change:

  • college_id column to college_ID,
  • college_name to name ,
  • college_address to address and
  • Total Staff to staff.

Given below is code snippet to how to change multiple column names in pandas:

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

# change the multiple column name

data=data.rename(columns={'college_id':'college_ID','college_name':'name','college_address':'address','Total Staff':'staff'})

#display dataframe

print(data)

Output: After change in multiple column names in pandas for above example we obtain below:

         college_ID               name address  staff
college1      c-001  vignan university  guntur   1200
college2      c-021               vvit  guntur   3422
college3      c-002           RVR - JC  guntur   5644
college4      c-004  Andhra University  guntur    670

We have used rename function to change column names in pandas dataframe or rename column names in pandas dataframe, and rename multiple column names in pandas.

 

Now lets see other way using columns method.


Method 2change column names in pandas using columns

Here , we are going to use a list of columns and assign it to the columns method. So it will assign the new columns to the dataframe.

Syntax:

dataframe.columns=[‘new_columns’]

where, dataframe is the input dataframe.

 

Examplechange multiple column names in pandas

In this example, we are going to change college_id column to college_ID, college_name to name, college_address to address and Total Staff to staff.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

# change the multiple column name

data.columns=['college_ID','name','address','staff']

#display dataframe

print(data)

Output: Given below is the result obtained after we rename multiple column names in pandas as required.  

         college_ID               name address  staff
college1      c-001  vignan university  guntur   1200
college2      c-021               vvit  guntur   3422
college3      c-002           RVR - JC  guntur   5644
college4      c-004  Andhra University  guntur    670

We have seen how to change all column names in pandas dataframe using columns. Lets move ahead to third method.


Method 3change column names in pandas using set_axis()

Here , we are going to use a list of columns and assign it to the set_axis method. So it will assign the new columns to the dataframe.

Syntax:

dataframe.set_axis([new_columns],axis=1)

where,

1.      dataframe is the input dataframe.

2.      axis=1 represents column

 

Examplerename multiple column names in pandas

In this example, we are going to change college_id column to college_IDcollege_name to name , college_address to address and Total Staff to staff.

 

Lets see how to change all column names in pandas dataframe using below code snippet:

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])

# change the multiple column name

data=data.set_axis(['college_ID','name','address','staff'],axis=1)

#display dataframe

print(data)

Outputchange multiple column names in pandas is done and result is presented below:

         college_ID               name address  staff
college1      c-001  vignan university  guntur   1200
college2      c-021               vvit  guntur   3422
college3      c-002           RVR - JC  guntur   5644
college4      c-004  Andhra University  guntur    670

Lets see the other method to change column names in pandas dataframe.


Method 4 : change column names in pandas using str.replace()

This method str.replace will replace each column name at a time using columns method

Syntax:

data.columns=data.columns.str.replace('old_column', ‘new_column')

where,

1.      data is the input dataframe

2.      old_column is the old column name

3.      new_column is the new column name

 

Examplechange multiple column names in pandas using str.replace

In this example, we are going to change college_id column to college_ID, college_name to name , college_address to address and Total Staff to staff.

import pandas as pd

#create dataframe from the college data

data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['college1','college2','college3','college4'])



# change multiple column names in pandas

data.columns=data.columns.str.replace('college_id','college_ID')

data.columns=data.columns.str.replace('college_name','name')

data.columns=data.columns.str.replace('college_address','address')

data.columns=data.columns.str.replace('Total Staff','staff')

#display dataframe

print(data)

Output: We get following result after rename multiple column names in pandas as per above code:

         college_ID               name address  staff

college1      c-001  vignan university  guntur   1200

college2      c-021               vvit  guntur   3422

college3      c-002           RVR - JC  guntur   5644

college4      c-004  Andhra University  guntur    670

As you can see, we can change multiple column names in pandas or single column name using str.replace easily.


Conclusion:

In this article, we discussed about change column names in pandas dataframe\rename column names in pandas dataframe and how to change all column names in pandas dataframe with four methods - rename(), set_axis(), str.replace() and columns methods.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Feb 24,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!