mapper | Dict to update the names along the axis. |
index | Specify index to rename |
columns | Dict to Specify columns to rename |
axis | Axis name or number ( 0, 1 ) to target the rename |
inplace | Bool, if set to True then original DataFrame changed |
level | For multi Index , rename is specified level. |
errors | default is 'ignore', can be set to 'raise' |
import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],'MATH':[30,40,50,60,70,80],
'ENGLISH':[20,30,40,50,60,70]}
my_data = pd.DataFrame(data=my_dict)
my_data1=my_data.rename(columns={'NAME':'name1','ENGLISH':'eng'})
print(my_data1)
Output
name1 ID MATH eng
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
5 Jack 6 80 70
inplace=True. Here we are changing the index keys ( first two , 0 and 1 )
my_data.rename(index={0:'a',1:'b'},inplace=True)
print(my_data)
Output
NAME ID MATH ENGLISH
a Ravi 1 30 20
b Raju 2 40 30
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
5 Jack 6 80 70
str.lower. We will add axis=1 to our code.
my_data.rename(str.lower,axis=1,inplace=True)
print(my_data)
Output
name id math english
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 Ron 4 60 50
4 King 5 70 60
5 Jack 6 80 70
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.