The docs on pandas.DataFrame.replace says you have to provide a nested dictionary: the first level is the column name for which you have to provide a second dictionary with substitution pairs.

So, this should work:

>>> df=pd.DataFrame({'a': ['NÍCOLAS','asdč'], 'b': [3,4]})
>>> df
         a  b
0  NÍCOLAS  3
1     asdč  4

>>> df.replace({'a': {'č': 'c', 'Í': 'I'}}, regex=True)
         a  b
0  NICOLAS  3
1     asdc  4

Edit. Seems pandas also accepts non-nested translation dictionary. In that case, the problem is probably with character encoding, particularly if you use Python 2. Assuming your CSV load function decoded the file characters properly (as true Unicode code-points), then you should take care your translation/substitution dictionary is also defined with Unicode characters, like this:

dictionary = {u'í': 'i', u'á': 'a'}

If you have a definition like this (and using Python 2):

dictionary = {'í': 'i', 'á': 'a'}

then the actual keys in that dictionary are multibyte strings. Which bytes (characters) they are depends on the actual source file character encoding used, but presuming you use UTF-8, you'll get:

dictionary = {'\xc3\xa1': 'a', '\xc3\xad': 'i'}

And that would explain why pandas fails to replace those chars. So, be sure to use Unicode literals in Python 2: u'this is unicode string'.

On the other hand, in Python 3, all strings are Unicode strings, and you don't have to use the u prefix (in fact unicode type from Python 2 is renamed to str in Python 3, and the old str from Python 2 is now bytes in Python 3).

Answer from randomir on Stack Overflow
Top answer
1 of 4
19

The docs on pandas.DataFrame.replace says you have to provide a nested dictionary: the first level is the column name for which you have to provide a second dictionary with substitution pairs.

So, this should work:

>>> df=pd.DataFrame({'a': ['NÍCOLAS','asdč'], 'b': [3,4]})
>>> df
         a  b
0  NÍCOLAS  3
1     asdč  4

>>> df.replace({'a': {'č': 'c', 'Í': 'I'}}, regex=True)
         a  b
0  NICOLAS  3
1     asdc  4

Edit. Seems pandas also accepts non-nested translation dictionary. In that case, the problem is probably with character encoding, particularly if you use Python 2. Assuming your CSV load function decoded the file characters properly (as true Unicode code-points), then you should take care your translation/substitution dictionary is also defined with Unicode characters, like this:

dictionary = {u'í': 'i', u'á': 'a'}

If you have a definition like this (and using Python 2):

dictionary = {'í': 'i', 'á': 'a'}

then the actual keys in that dictionary are multibyte strings. Which bytes (characters) they are depends on the actual source file character encoding used, but presuming you use UTF-8, you'll get:

dictionary = {'\xc3\xa1': 'a', '\xc3\xad': 'i'}

And that would explain why pandas fails to replace those chars. So, be sure to use Unicode literals in Python 2: u'this is unicode string'.

On the other hand, in Python 3, all strings are Unicode strings, and you don't have to use the u prefix (in fact unicode type from Python 2 is renamed to str in Python 3, and the old str from Python 2 is now bytes in Python 3).

2 of 4
9

replace works out of the box without specifying a specific column in Python 3.

Load Data:

df=pd.read_csv('test.csv', sep=',', low_memory=False, encoding='iso8859_15')
df

Result:

col1    col2
0   he  hello
1   Nícolas shárk
2   welcome yes

Create Dictionary:

dictionary = {'í':'i', 'á':'a'}

Replace:

df.replace(dictionary, regex=True, inplace=True)

Result:

 col1   col2
0   he  hello
1   Nicolas shark
2   welcome yes
🌐
Statology
statology.org › home › pandas: how to remove special characters from column
Pandas: How to Remove Special Characters from Column
October 10, 2022 - Hi Ritesh…To remove the second occurrence of special characters from a column in a Pandas DataFrame, you can use regular expressions (regex) and apply it to the column of interest.
Discussions

Python Pandas Replace Special Character - Stack Overflow
Don't worry, you're not alone--unicode ... in Python 3. If all you're concerned about is the ñ, you should decode in UTF-8, and then just replace the one character. ... Note here that u'\xf1' uses the hex escape for the character in question. I was informed in the comments that <>.str.replace is a pandas series method, ... More on stackoverflow.com
🌐 stackoverflow.com
June 19, 2018
How to efficiently remove a list of special characters from a pandas dataframe?
It's definitely faster. You can even pass in lists or dicts to control what changes to what. More on reddit.com
🌐 r/learnpython
4
9
September 29, 2023
python - Replace or Remove special characters such as ' and " in pandas dataframe - Stack Overflow
In the data frame that I am working on, there are several columns that contain special characters such as " and ' . They are either at the end or in the beginning of the column name. How can I... More on stackoverflow.com
🌐 stackoverflow.com
July 22, 2022
python - How to replace list of special characters to a single char in pandas dataframe - Stack Overflow
16 Replacing special characters in pandas dataframe · 1 how to remove special characters in pandas dataframe · 1 Iterate through a pandas dataframe (column by column) and extract all special characters into a list · 1 How to remove special characters from the column values using python More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
8

I'm assuming you're using Python 2.x here and this is likely a Unicode problem. Don't worry, you're not alone--unicode is really tough in general and especially in Python 2, which is why it's been made standard in Python 3.

If all you're concerned about is the ñ, you should decode in UTF-8, and then just replace the one character.

That would look something like the following:

DF['name'] = DF['name'].str.decode('utf-8').replace(u'\xf1', 'n')

As an example:

>>> "sureño".decode("utf-8").replace(u"\xf1", "n")
u'sureno'

If your string is already Unicode, then you can (and actually have to) skip the decode step:

>>> u"sureño".replace(u"\xf1", "n")
u'sureno'

Note here that u'\xf1' uses the hex escape for the character in question.

Update

I was informed in the comments that <>.str.replace is a pandas series method, which I hadn't realized. The answer to this possibly might be something like the following:

DF['name'] = map(lambda x: x.decode('utf-8').replace(u'\xf1', 'n'), DF['name'].str)

or something along those lines, if that pandas object is iterable.

Another update

It actually just occurred to me that your issue may be as simple as the following:

DF['NAME']=DF['NAME'].str.replace(u"ñ","n")

Note how I've added the u in front of the string to make it unicode.

2 of 2
0

You can use replace function with special character to be replaced with a different value of your choice in the following way.

if your dataframe is df and you have to do it in all the columns that are string. in case of mine I am doing it for "\n"

df= df.applymap(lambda x: x.replace("\n"," "))
🌐
Reddit
reddit.com › r/learnpython › how to efficiently remove a list of special characters from a pandas dataframe?
r/learnpython on Reddit: How to efficiently remove a list of special characters from a pandas dataframe?
September 29, 2023 -

I have a program that loops through each column, and a nested loop that goes through the list of bad characters and converts each column to a str using astype() and then replace(). I also use re.escape() inside my replace method.

I noticed on large data frames of 100k or 500k rows it takes a long time.

I wonder if applying replace() on the entire dataframe is more reliable and faster? Like in the below example:

df = df.replace()

My concern is if replace() will guarantee removing the characters regardless of the data type in each column? I need assurance that the special characters are truly removed from the dataframe. My method right now is reliable but I’ve noticed it’s slow with large data frames so I would like someone with experience to help me refactor if possible.

🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.17.0 › text.html
Working with Text Data — pandas 0.17.0 documentation
# Consider the following badly formatted financial data In [25]: dollars = pd.Series(['12', '-$10', '$10,000']) # This does what you'd naively expect: In [26]: dollars.str.replace('$', '') Out[26]: 0 12 1 -10 2 10,000 dtype: object # But this doesn't: In [27]: dollars.str.replace('-$', '-') Out[27]: 0 12 1 -$10 2 $10,000 dtype: object # We need to escape the special character (for >1 len patterns) In [28]: dollars.str.replace(r'-\$', '-') Out[28]: 0 12 1 -10 2 $10,000 dtype: object ·
🌐
Bobby Hadz
bobbyhadz.com › blog › pandas-remove-special-characters-from-column
Pandas: Remove special characters from Column Values/Names | bobbyhadz
April 12, 2024 - Notice that we also set the re.IGNORECASE flag in the call to str.replace(). This makes our match case-insensitive by targeting all uppercase and lowercase characters. The current regular expression also considers spaces to be special characters. ... Copied!import re import pandas as pd df = pd.DataFrame({ '$name$': ['Ali# c_e', 'Bo_b by@', 'Ca$r %l', 'D^a &n'], '!experience@': [11, 14, 16, 18], '^salary*': [175.1, 180.2, 190.3, 210.4], }) df['$name$'] = df['$name$'].str.replace( r'[^a-z0-9]', '', regex=True, flags=re.IGNORECASE ) # $name$ !experience@ ^salary* # 0 Alice 11 175.1 # 1 Bobby 14 180.2 # 2 Carl 16 190.3 # 3 Dan 18 210.4 print(df)
🌐
GeeksforGeeks
geeksforgeeks.org › python › pandas-remove-special-characters-from-column-names
Pandas - Remove special characters from column names - GeeksforGeeks
September 5, 2020 - # 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)
Find elsewhere
🌐
Pythonhelpdesk
pythonhelpdesk.com › 2024 › 02 › 25 › data-frame-replace-special-characters-using-str-replace
Data Frame: Replace Special Characters Using `str.replace()` – Python HelpDesk
Import the pandas library as pd for DataFrame operations. Create a sample DataFrame with text columns containing special characters. Utilize .str.replace() with a regex pattern (r'[@#$]’) to remove occurrences of ‘@’, ‘#’, and ‘$’.
🌐
CSDN
devpress.csdn.net › python › 63045ef9c67703293080bf99.html
Replacing special characters in pandas dataframe_python_Mangs-Python
August 23, 2022 - >>> df=pd.DataFrame({'a': ['NÍCOLAS','asdč'], 'b': [3,4]}) >>> df a b 0 NÍCOLAS 3 1 asdč 4 >>> df.replace({'a': {'č': 'c', 'Í': 'I'}}, regex=True) a b 0 NICOLAS 3 1 asdc 4 · Edit. Seems pandas also accepts non-nested translation dictionary. In that case, the problem is probably with character encoding, particularly if you use Python 2.
🌐
Sling Academy
slingacademy.com › article › pandas-remove-special-characters-and-whitespace-from-column-names
Pandas: Remove special characters and whitespace from column names - Sling Academy
This code cleans each column name by stripping leading and trailing spaces, replacing interior spaces with underscores, and removing special characters, all in a neat, reusable function. When dealing with larger datasets or more complex scenarios, you might want to automate the cleaning process further. One way is to leverage the power of list comprehensions combined with regex: import pandas as pd import re df = pd.DataFrame({"name with space": range(5), "@special*char#column": range(5)}) df.columns = [re.sub(r"[^\w\s]", "", col).replace(" ", "_") for col in df.columns] print(df.head())
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.5 documentation
Replace each occurrence of pattern/regex in the Series/Index · Equivalent to str.replace() or re.sub(), depending on the regex value
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-characters-in-strings-in-pandas-dataframe
Replace Characters in Strings in Pandas DataFrame - GeeksforGeeks
July 23, 2025 - Example 1: The following program is to replace a character in strings for the entire dataframe. ... # import pandas import pandas as pd data = {'Student_Full_Name': ['Mukul_Jatav', 'Rahul_Shukla', 'Robin_Singh', 'Mayank_Sharma', 'Akash_Verma'], 'Father_Full_name': ['Mukesh_Jatav', 'Siddhart_Shukla', 'Rohit_Singh', 'Sunil_Sharma', 'Rajesh_Verma'] } # create an dataframe df = pd.DataFrame(data, columns=['Student_Full_Name', 'Father_Full_name']) # print dataframe print(" original dataframe \n", df) # replace '_' with '-' df = df.replace('_', '+', regex=True) # print dataframe print(" After replace character \n", df)