In this post, we will learn, how to Rename mutiple columns with list in Pandas. 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 mutiple columns with list in Pandas
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
2. Rename Mutiple columns with list 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 new column names as a list to set_axis() method and set 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)
list_new_columns = ['stu_Name', 'Admission_Fee', 'Ranks','Enroll_Fee']
dfobj.set_axis(list_new_columns, 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
3. Rename multiple columns with list in Pandas using index
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
4. Rename multiple columns with list 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
Dataframe 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