Pandas rename column

How can we rename columns in pandas Dataframe? There are three simplest methods to pandas rename column. By using rename function By using dot column (.column) function. By setting axis Table of Contents 1. By …

Pandas rename column

How can we rename columns in pandas Dataframe?

There are three simplest methods to pandas rename column.

  1. By using rename function
  2. By using dot column (.column) function.
  3. By setting axis

1. By using rename function

we can use rename function in two ways to rename a column.

1.1 By creating a new Data Frame.

1.2 By using the same Data Frame.

1.1 Create another data frame.

Syntax:

df2 = df.rename(columns={‘existing col name’ : ‘new name’})

And run this with df2 and it will print the output in which column name has changed.

(Here df is representing a Data Frame).

1.2 You don’t have to create another data frame you can rename just by adding one parameter into the existing data frame.

Inplace= True. 

Like:

df.rename(columns={‘existing col name’ : ‘new name’}, inplace= True)

Let’s understand this more clearly with the help of example:

Example:

import pandas as pd
data = {'IT':['raj', 'karan', 'sameer', 'anil'],
         'SW':['shweta', 'anika', 'preeti', 'sahil'],
         'EE':['Hina', 'sana', 'mike', 'imam']
}
df = pd.DataFrame(data)
df

As it is a rename column function in pandas.

First, we have imported pandas as “pd” and then we have created a dictionary and named it “data” after that used data frame object to print this.

Output:

               IT	 SW    	EE
0	raj	shweta	Hina
1	karan	anika	sana
2	sameer	preeti	mike
3	anil	sahil	imam

Now, lets apply rename function to rename a column by creating a new DataFrame object.

df2 = df.rename(columns={'EE':'CS'})
df2

Output:

             IT	SW	CS
0	raj	shweta	Hina
1	karan	anika	sana
2	sameer	preeti	mike
3	anil	sahil	imam

Let’s apply rename function to rename a column without creating another data frame.

df.rename(columns={'SW':'ARTS'}, inplace=True)
df
             IT	ARTS	CS
0	raj	shweta	Hina
1	karan	anika	sana
2	sameer	preeti	mike
3	anil	sahil	imam

Output:

If you want to make your column names in upper case or in lower case, so you can also do this with this.

To change column names into lower-case use str.lower

df = df.rename(columns= str.lower)
df

Output:

	it	arts	cs
0	raj	shweta	Hina
1	karan	anika	sana
2	sameer	preeti	mike
3	anil	sahil	imam

To change column names into upper-case use str.upper

df = df.rename(columns= str.upper)
df

Output:

         IT	 ARTS	CS
0	raj	shweta	Hina
1	karan	anika	sana
2	sameer	preeti	mike
3	anil	sahil	imam

Similarly, you can rename multiple column names by using these methods.

2. By using dot column function

To rename a column in pandas we can also use “.column” method, for that we do not have to create another dataframe we can use “.column” function within the same dataframe

Syntax:

 df= columns = [‘col1’, ‘col2’, ‘col4’, ‘col5’]

and run this with “df”.

And this will print the column names as we stated in statement.

Let’s check this out in example.

data = {'IT':['raj', 'karan', 'sameer', 'anil'],
         'SW':['shweta', 'anika', 'preeti', 'sahil'],
         'EE':['Hina', 'sana', 'mike', 'imam'],
          'PG':['amir', 'akbar', 'jenny', 'prem']
}
df3 = pd.DataFrame(data)
df3


df3

 Extended the previous dictionary and dataframe object 3 as df3.

Output:

        IT	SW	EE	PG
0	raj	shweta	Hina	amir
1	karan	anika	sana	akbar
2	sameer	preeti	mike	jenny
3	anil	sahil	imam	prem

Let’s apply .columns function to rename

df3.columns = ['IT', 'SW', 'CS', 'LLB']
df3

Output:

        IT	SW	CS	LLB
0	raj	shweta	Hina	amir
1	karan	anika	sana	akbar
2	sameer	preeti	mike	jenny
3	anil	sahil	imam	prem

3. By setting axis

You can also change the column name in pandas by setting axis.

Syntax:

df.set_axis([‘col1’, ‘col2’, ‘col3’, ‘col4’], axis=1)

Let’s check how it works.

df3.set_axis(['IT','SW','EE','PG'], axis=1)

Output:

        IT	SW	EE	PG
0	raj	shweta	Hina	amir
1	karan	anika	sana	akbar
2	sameer	preeti	mike	jenny
3	anil	sahil	imam	prem

In the above example we printed the same table that we created in dictionary by setting axis method.

Now, lets rename the columns in panda by setting the axis method.

df3.set_axis(['IT','SW','CS','LLB'], axis=1)

Output:

        IT	SW	CS	LLB
0	raj	shweta	Hina	amir
1	karan	anika	sana	akbar
2	sameer	preeti	mike	jenny
3	anil	sahil	imam	prem

Conclusion

Pandas rename column” as we all know that pandas is python library.

And when we work in pandas data frame it contains two-dimensional data rows and columns and, in this article, we have discussed three simplest methods that you can use to rename column in pandas.

Suggested Read: Remove Brackets from list Python

Leave a Comment