Friday, September 5, 2025
HomeLanguagesPandas – Remove special characters from column names

Pandas – Remove special characters from column names

Let us see how to remove special characters like #, @, &, etc. from column names in the pandas data frame.  Here we will use replace function for removing special character.

Example 1: remove a special character from column names

Python




# import pandas
import pandas as pd
  
# create data frame
Data = {'Name#': ['Mukul', 'Rohan', 'Mayank',
                  'Shubham', 'Aakash'],
          
        'Location': ['Saharanpur', 'Meerut', 'Agra',
                     'Saharanpur', 'Meerut'],
          
        'Pay': [25000, 30000, 35000, 40000, 45000]}
  
df = pd.DataFrame(Data)
  
# print original data frame
print(df)
  
# remove special character
df.columns = df.columns.str.replace('[#,@,&]', '')
  
# print file after removing special character
print("\n\n", df)


Output:

Here, we have successfully remove a special character from the column names. Now we will use a list with replace function for removing multiple special characters from our column names.

Example 2: remove multiple special characters from the pandas data frame

Python




# import pandas
import pandas as pd
  
# create data frame
Data = {'Name#': ['Mukul', 'Rohan', 'Mayank',  
                 'Shubham', 'Aakash'],  
          
        'Location@' : ['Saharanpur', 'Meerut', 'Agra',  
                      'Saharanpur', 'Meerut'],
          
        'Pay&' : [25000,30000,35000,40000,45000]}  
  
df=pd.DataFrame(Data)
  
# print original data frame
print(df)
  
# remove special character
df.columns=df.columns.str.replace('[#,@,&]','')
  
# print file after removing special character
print("\n\n" , df)


Output:

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS