DataFrame.add_prefix('str')
str | String to be prefixed to column names . |
Using DataFrame
We are trying to change the column NAME to name1
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_data=my_data.add_prefix('Col_')
print(my_data)
Output
Col_NAME Col_ID Col_MATH Col_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
Using series
my_s=pd.Series([1,5,7,9,10])
my_s=my_s.add_prefix('Ser_')
print(my_s)
Output
Ser_0 1
Ser_1 5
Ser_2 7
Ser_3 9
Ser_4 10
dtype: int64
« Pandas
columns() rename() add_suffix()
← Subscribe to our YouTube Channel here