In this post, we will learn how to rename columns in Pandas DataFrame.The pandas dataframe rename() functions columns parameter takes a dictionary of key-value pairs for new column names. The dictionary map old columns of the dataframe to a new column that is given in the dictionary. The parameter inplace=True indicates that we are making changes in the original dataframe without creating a copy.
1. Rename a single column in Pandas
In this example, we will understand how to rename a single column of Pandas DataFrame.We have passed a dictionary {‘Fee’:’Admission_Fee’) to the columns parameter of rename() function where the key contains the old column names and the value is 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 = {'Fee':'Admission_Fee'}, inplace = True)
print("Dataframe after rename Column:\n", dfobj.columns)
Output
Dataframe after rename Column:
Index(['Name', 'Marks', 'Admission_Fee', 'Tution_Fee'], dtype='object')
- Add one or multiple columns to Pandas DataFrame
- Fill nan values of multiple columns in Pandas
- How to ffill missing value in Pandas
- Add mutiple columns to Pandas DataFrame
- Add numpy array to Pandas Dataframe as column
- Append List as a row in Pandas Dataframe
2. Rename Mutiple columns in Pandas
To rename single or multiple columns in Pandas Dataframe rename() method is used. In which We assign a dictionary of multiple column name as key-value pair to columns parameter of rename() function of as shown in below program the dictionary is {‘Name’:’stu_Name’,’Fee’:’Admission_Fee’,’Marks’:’Ranks’} where in dictionary
- Key: represents the old column names of Pandas dataframe
- 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 = {'Name':'stu_Name','Fee':'Admission_Fee','Marks':'Ranks'}, inplace = True)
print("Dataframe after rename Column:\n", dfobj.columns)
print('\n',dfobj)
Output
Dataframe after rename Column:
Index(['stu_Name', 'Ranks', 'Admission_Fee', 'Tution_Fee'], dtype='object')
stu_Name Ranks Admission_Fee Tution_Fee
0 Rama 97 100 400
1 Rama 97 100 400
2 Max 100 300 600
3 Rama 97 100 400
3. Rename all columns in Pandas DataFrame with a list
In this example we will discuss How to rename columns in Pandas DataFrame or rename all columns of the Pandas dataframe, We have to assign a list of the new column names to Pandas. columns properties as shown in the below program.The steps we have to follow
- Import Pandas Library using import pandas as PD
- Create a DataFrame using pd.dataframe from a dictionary
- Assign a list of new column names
- Print the updated 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 = ['stu_Name', 'Admission_Fee', 'Ranks','Enroll_Fee']
print("Dataframe after rename Column:\n", dfobj.columns)
print('\n',dfobj)
Output
Dataframe after rename Column:
Index(['stu_Name', 'Admission_Fee', 'Ranks', 'Enroll_Fee'], dtype='object')
stu_Name Admission_Fee Ranks Enroll_Fee
0 Rama 97 100 400
1 Rama 97 100 400
2 Max 100 300 600
3 Rama 97 100 400
4. Rename Mutiple columns in Pandas using set_axis()
The set_axis() method is used a set the index per the requested axis. Indexes for column or row labels can be changed by assigning a list-like Index. In this example, we have used set_axis() to rename the multiple columns of Pandas dataframe and passed them as a list, and axis=’columns’
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.set_axis(['stu_Name', 'Admission_Fee', 'Ranks','Enroll_Fee'], axis='columns', inplace=True)
print("Dataframe after rename Column:\n", dfobj.columns)
print('\n',dfobj)
Output
Dataframe after rename Column:
Index(['stu_Name', 'Admission_Fee', 'Ranks', 'Enroll_Fee'], dtype='object')
stu_Name Admission_Fee Ranks Enroll_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 in Pandas DataFrame by suffix and Prefix
Sometimes instead of renaming the column name, it needs to replace the suffix and prefix of the column. The Pandas dataframe add_prefix() method is used to add a prefix to the column name. The Pandas dataframe add_suffix() to rename columname by adding a suffix to columname.
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)
print("Dataframe after rename Column:\n", dfobj.columns)
dfobj = dfobj.add_prefix('Stu')
dfobj = dfobj.add_suffix('_D')
print('\n',dfobj)
Output
Dataframe after rename Column:
Index(['Name', 'Marks', 'Fee', 'Tution_Fee'], dtype='object')
StuName_D StuMarks_D StuFee_D StuTution_Fee_D
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 in Pandas DataFrame by index
To rename by index, we have accessed the column by index and assigned a new column name. This is how we rename columname by index.
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')
7. How to rename columns in Pandas DataFrame using read.csv()
While creating a dataframe from a CSV file, to rename the column we have to pass new columns name as a list to names=[‘New_col1’, ‘new_col2’, ‘new_coln’] parameter of read_csv() method.
import pandas as pd
dfobj = pd.read_csv('devenum.csv',
names=['New_col1', 'new_col2', 'new_coln'],
header=0)
8.Rename multiple columns in Pandas DataFrame using str.replace()
We can rename a single character of a single and multiple column of Pandas str.replace() method
- for single column: dfobj.columns.str.replace(‘old_col’, ‘new_col’)
- for multiple columns : dfobj.columns.str.replace(r”[_t]”, “”)
import pandas as pd
data = {
'Name_t': ['Rama', 'Rama', 'Max', 'Rama'],
'Marks_t':[97,97,100,97],
'Fee_t':[100,100,300,100],
'Tution_Fee_t':[400,400,600,400]
}
dfobj = pd.DataFrame(data)
dfobj.columns = dfobj.columns.str.replace(r"[_t]", "")
print("Dataframe after rename Column:\n", dfobj.columns)
Output
Dataframe after rename Column:
Index(['Name', 'Marks', 'Fee', 'TuionFee'], dtype='object')