I have a dataframe like as shown below
JavaScript
x
5
1
ID,US-Test1,US-Test2,US-Test3
2
1,11,12,13
3
2,13,16,18
4
3,15,19,21
5
I would like to remove the keyword US -
from all my column names
I tried the below but there should be better way to do this
JavaScript
1
6
1
newNames = {
2
'US-Test1':'Test1',
3
'US-Test2':'Test2'
4
}
5
df.rename(columns=newNames,inplace=True)
6
But my real data has 70 plus columns and this is not efficient.
Any regex approach to rename columns based on regex to exclude the pattern and retain only what I want?
I expect my output to be like as shown below
JavaScript
1
5
1
ID,Test1,Test2,Test3
2
1,11,12,13
3
2,13,16,18
4
3,15,19,21
5
Advertisement
Answer
You could use a regex that matches the “US-” at the beginning like this:
JavaScript
1
2
1
df.columns = df.columns.str.replace("^US-", "", regex=True)
2
It replaces the matching “US-” with an empty string.
Also, if you know the columns that you want to transform you could apply slicing on their names to remove the first 3 characters:
JavaScript
1
2
1
df.columns = df.columns.str.slice(3)
2
Of course, this will affect columns that do not match your condition (i.e. do not begin with “US-“)