Given that this is the top Google result when searching for "Pandas replace is not working" I'd like to also mention that:

replace does full replacement searches, unless you turn on the regex switch. Use regex=True, and it should perform partial replacements as well.

This took me 30 minutes to find out, so hopefully I've saved the next person 30 minutes.

Answer from Reddspark on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › pandas .replace not working
r/learnpython on Reddit: pandas .replace not working
December 31, 2015 -

This is genuinely driving me crazy.

I have a data frame of unit prices in string format i'm trying to get them to a float

item_df['Unit Price'] = item_df['Unit Price'].replace('$','')

and all the '$' are still there.

THEN when I do this:

item_df['Unit Price'][1] = item_df['Unit Price'][1].replace('$','')

The '$' is gone from that index ಠ_ಠ. What the hell is going on?? Am I taking crazy pills or missing some fundamental concept?

Any help would be much appreciated.

Thanks,

Discussions

Why doesn't this replacement change absolutely all entries meeting criteria?
I tried this on a dataframe df that has one column containing the strings “A” and “B”. I want to change B to 1 and A to 0. Here is how I tried: # Define the mapping dictionary mapping = {'A': 0, 'B': 1} # Replace values in the entire DataFrame using the mapping dictionary ... More on discuss.python.org
🌐 discuss.python.org
11
0
May 1, 2024
dataframe.replace() does not work on a subset of rows
My thought is that the last line of the above code block should result in a data frame that has all the 01's and 02's replaced for all rows that have 'ab' == 'A'. But this does not work. No exception is thrown. More on github.com
🌐 github.com
4
March 11, 2017
How to apply replace to whole DataFrame like in Python?
To replace a character in df in Python we can: df.replace("?", np.nan, inplace = True) but it seems Julia not support replace to the whole DataFrame replace(df, "?" => missing) returns: MethodError: no method matching similar(::DataFrames.DataFrame, ::Type{Any}) After some testing, I found ... More on discourse.julialang.org
🌐 discourse.julialang.org
7
0
May 13, 2023
BUG: Pandas DataFrame replace() doesn't work if the dataframe has nullable boolean columns
I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of pandas. I have confirmed this bug exists on the master branch of pandas. Repr... More on github.com
🌐 github.com
2
November 17, 2021
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.6 documentation
When regex=True, value is not None and to_replace is a string, the replacement will be applied in all columns of the DataFrame. >>> df = pd.DataFrame( ... { ... "A": [0, 1, 2, 3, 4], ... "B": ["a", "b", "c", "d", "e"], ... "C": ["f", "g", "h", "i", "j"], ...
🌐
Medium
medium.com › @whyamit101 › understanding-pandas-replace-function-c2b0b7709233
Understanding pandas replace() Function | by why amit | Medium
February 10, 2025 - Let’s take a look at how replace() works in different situations: ... If you want to replace one specific value with another, you can just pass those values directly into the function. For example, if you have a column with the value 3 that needs to be changed to 10, here's how you can do it: import pandas as pd df = pd.DataFrame({'A': [1, 2, 3, 4, 5]}) df = df.replace(3, 10) print(df)
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › pandas replace() – replace values in pandas dataframe
Pandas replace() - Replace Values in Pandas Dataframe • datagy
March 2, 2023 - However, it’s not my preferred approach as the behavior can often be difficult to read. Let’s take a look at how the method can replace values: # Using a Dictionary (Dict is passed into to_replace=) df['Age'] = df['Age'].replace({23:99, 45:999}) # Using a Dictionary for Column Replacements (key:value = column:value) df = df.replace({'Name': 'Jane', 'Age': 45}, 99) We can see that the dictionary can be used in two different ways: To map values to replace so that the dictionary represents {original value : new value} To map replacements from columns so that it follows the structure shown here: to_replace={column1: value1, column2: value2}, value=new value ·
🌐
Python.org
discuss.python.org › python help
Why doesn't this replacement change absolutely all entries meeting criteria? - Python Help - Discussions on Python.org
May 1, 2024 - I tried this on a dataframe df that has one column containing the strings “A” and “B”. I want to change B to 1 and A to 0. Here is how I tried: # Define the mapping dictionary mapping = {'A': 0, 'B': 1} # Replace values in the entire DataFrame using the mapping dictionary df.replace(mapping, inplace=True) print(df) But when I look at the entries in that (after exporting to a csv so I can read it), it almost does the job.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 15656
dataframe.replace() does not work on a subset of rows · Issue #15656 · pandas-dev/pandas
March 11, 2017 - IndexingRelated to indexing on series/frames, not to indexes themselvesRelated to indexing on series/frames, not to indexes themselvesUsage Question ... # Your code here data = pd.DataFrame( {'ab' : ['A','B','A','A','B'], 'num' : ['01','02','01','01','01']}) a_replacements = { 'num' : { '01' : 'funny', '02' : 'serious' }} b_replacements = { 'num' : { '01' : 'beginning', '02' : 'end' }} data[data.ab == 'A'].replace(inplace=True, to_replace=a_replacements)
Author: pandas-dev
🌐
GeeksforGeeks
geeksforgeeks.org › data analysis › python-pandas-dataframe-replace
Python | Pandas dataframe.replace() - GeeksforGeeks
# importing pandas as pd import pandas as pd # Making data frame from the csv file df = pd.read_csv("nba.csv") # will replace Nan value in dataframe with value -99999 df.replace(to_replace = np.nan, value =-99999) ... Notice all the Nan value in the data frame has been replaced by -99999.
Published: July 11, 2025
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › replace
Python Pandas DataFrame replace() - Replace Values | Vultr Docs
December 27, 2024 - Apply regex within replace() for pattern-based replacement. ... df = pd.DataFrame({ 'A': ['foo', 'bar', 'baz'], 'B': ['foobar', 'barfoo', 'foobarbaz'] }) df.replace(r'^foo', 'new', regex=True) Explain Code
🌐
Julia Programming Language
discourse.julialang.org › general usage
How to apply replace to whole DataFrame like in Python? - General Usage - Julia Programming Language
May 13, 2023 - To replace a character in df in Python we can: df.replace("?", np.nan, inplace = True) but it seems Julia not support replace to the whole DataFrame replace(df, "?" => missing) returns: MethodError: no method matching similar(::DataFrames.DataFrame, ::Type{Any}) After some testing, I found the alternative using for loop: for col in names(df) replace!(df[!, col], "?" => missing) end but actually I prefer one-liner code than for loop.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 44499
BUG: Pandas DataFrame replace() doesn't work if the dataframe has nullable boolean columns · Issue #44499 · pandas-dev/pandas
November 17, 2021 - Also please note both of the following work fine: import pandas as pd df = pd.DataFrame({"a": ["x", "y", "z", "w"], "b": [True, False, False, True]}).astype({"b":"bool"}) # notice 'bool' instead of 'boolean' df.replace("x", "X") and: df = pd.DataFrame({"a": ["x", "y", "z", "w"], "b": [True, False, False, True]}) df.replace("x", "X") the replace should complete successfully ·
Author: pandas-dev
🌐
GitHub
github.com › pandas-dev › pandas › issues › 54399
BUG: df.replace() with list of regex sometimes fails with column of null objects · Issue #54399 · pandas-dev/pandas
August 4, 2023 - I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of pandas. I have confirmed this bug exists on the main branch of pandas. import pandas as pd # FAILS df1 = pd.DataFrame({ "a": pd.Series([1], dtype="int64"), "b": pd.Series([None], dtype="object"), }) # PASSES df2 = pd.DataFrame({ "a": [1], "b": [None], }, dtype="object") # PASSES df3 = pd.DataFrame({ "a": pd.Series([1], dtype="object"), "b": pd.Series([None], dtype="object"), }) # PASSES df4 = pd.DataFrame({ "a": pd.Series([1], dtype="int64"), "b": pd.Series([None], dtype="obj
Author: pandas-dev
🌐
GitHub
github.com › pandas-dev › pandas › issues › 46606
BUG: DataFrame.replace with dict doesn't work when value=None · Issue #46606 · pandas-dev/pandas
April 1, 2022 - I have checked that this issue has not already been reported. I have confirmed this bug exists on the latest version of pandas. I have confirmed this bug exists on the main branch of pandas. import pandas as pd df = pd.DataFrame(dict(a=[1,2,3], b=[1,2,3])) df.replace({1:5}, value=None) # does not replace values at all df.replace({1:5}) # correctly replaces 1s with 5s
Author: pandas-dev
🌐
YouTube
youtube.com › watch
Solving the Issue of dataframe replace() Not Working Inside Functions in Python - YouTube
Discover why the `dataframe replace()` method fails within Python functions and learn how to fix this common problem easily.---This video is based on the que...
Published: April 7, 2025
Views: 0
🌐
Python Forum
python-forum.io › thread-33283.html
Pandas replace function not working on datafram with floats
April 12, 2021 - Hi, I am using Pandas' replace function and it works on one DataFrame but not on another and I don't understand why. I have tried many different solutions but nothing seems to work - Please take a look at the example code below: import pandas as p...
🌐
GitHub
github.com › pandas-dev › pandas › issues › 45372
BUG: Pandas.DataFrame.str.replace function fails silently for mixed data mixing strings and float/int and replaces with NaN · Issue #45372 · pandas-dev/pandas
January 14, 2022 - import pandas as pd import numpy as np list_of_random_numbers = np.random.randint(0,100,size=10) list_of_random_numbers_as_string = [str(x) for x in np.random.randint(0,100,size=10)] list_of_random_numbers_as_string[0] = list_of_random_numbers_as_string[0] + "'" df = pd.concat( [pd.DataFrame({'value':list_of_random_numbers}), pd.DataFrame({'value':list_of_random_numbers_as_string})], ignore_index=True) df['value'] = df['value'].str.replace("'", "") df.notnull().sum() When replacing a string in a column with mixed data, this replaces all non-string data with NaN without a warning (silent failure).
Author: pandas-dev
🌐
YouTube
youtube.com › the python oracle
replace() method not working on Pandas DataFrame - YouTube
--------------------------------------------------Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn--------------------...
Published: April 6, 2024
Views: 35
🌐
GitHub
github.com › pandas-dev › pandas › issues › 26050
Problem with DataFrame.replace using None · Issue #26050 · pandas-dev/pandas
April 11, 2019 - import pandas as pd import numpy as np ar = np.random.normal(size=[100,10]) df = pd.DataFrame(ar).astype(str) df.replace('0',None) Every time I run something similar to this (replacing, in a string DataFrame/DataFrame column, a specific string with None), the columns get filled with the element directly above it in the column; this creates a lot of confusion since None is, pretty much, THE "pythonic" way of signalling the absence of a variable/whatever. It works fine if replaced with np.nan, but if anything, I would expect them to work very approximately the same.
Author: pandas-dev
🌐
Stack Overflow
stackoverflow.com › questions › 70343534 › why-does-replace-not-work-in-my-dataframe
python - Why does .replace not work in my dataframe? - Stack Overflow
With regex=True it will replace commas contained within cells with other text. This is the standard way to solve this problem at the DataFrame level. ... Save this answer. ... Show activity on this post.