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,

🌐
Reddit
reddit.com › r/learnpython › why have to add regex = true to get .replace to work (pandas)
r/learnpython on Reddit: Why have to add regex = True to get .replace to work (pandas)
March 29, 2022 -

Hello Very new to pandas. Trying to replace ampersand in my excel file

Why did I have to add regex=True to get this to work. It wouldn’t update otherwise.

df = df.replace(‘%26’ , ‘&’ , regex = True)

🌐
Reddit
reddit.com › r/learnpython › vectorized .str.replace() for multiple characters in pandas
r/learnpython on Reddit: Vectorized .str.replace() for multiple characters in pandas
June 9, 2022 -

I have a dataframe:

 {'country': {0: 'Afghanistan?*', 1: 'Albania?*'},
 'region': {0: 'Asia', 1: 'Europe'},
 'subregion': {0: 'Southern Asia', 1: 'Southern Europe'},
 'rate_per_1000': {0: 6.7, 1: 2.1},
 'count': {0: '2,474', 1: '61'},
 'year': {0: 2018, 1: 2020},
 'source': {0: 'NSO', 1: 'NSO'}}

          country  region        subregion  rate_per_1000  count  year source
0   Afghanistan?*    Asia    Southern Asia            6.7  2,474  2018    NSO
1       Albania?*  Europe  Southern Europe            2.1     61  2020    NSO

There are multiple bad characters here that I want to get rid of. I made a short function for .apply() to get rid of them, however I am looping over a defined list of bad characters. This gives a bad code smell to me, I think this operation could be more vectorized in some way. This is what I've tried:

bad_chars = ['?', '*', ',']

def string_cleaner(col):
    if col.dtype == 'object':
        for char in bad_chars:
            col = col.str.replace(f'{char}', '')
        return col

homicide_by_country = homicide_by_country.apply(string_cleaner)

homicide_by_country
        country  region        subregion rate_per_1000 count  year source
0   Afghanistan    Asia    Southern Asia          None  2474  None    NSO
1       Albania  Europe  Southern Europe          None    61  None    NSO

My desired outcome is a more pythonic/pandonic technique for accomplishing the same outcome.

You may notice for some reason my rate_per_1000 columns goes blank. I haven't troubleshot that problem yet but if you spot something obvious I'm all ears.

🌐
GitHub
github.com › pandas-dev › pandas › issues › 16808
Series.str.replace() is not actually the same as str.replace() · Issue #16808 · pandas-dev/pandas
June 30, 2017 - ... "When repl is a string, every pat is replaced as with str.replace()" However, that's not what is happening - it appears it's interpreting a string as a regex, so you need to escape characters like parentheses.
Author: pandas-dev
🌐
Reddit
reddit.com › r/learnpython › search and replace strings in dataframe
r/learnpython on Reddit: Search and Replace Strings in Dataframe
June 20, 2024 -

Dears

I do have a dataframe:

data1 = [['aaa567'], ['bbb333'], ['ccc23432']]
df1 = pd.DataFrame(data1, columns=['text'])

I do have a second dataframe

data2 = [['peter', 567], ['paul', 333], ['mary', 23432]]
df2 = pd.DataFrame(data2, columns=['name', 'number'])

text
0 aaa567
1 bbb333
2 ccc23432

name number
0 peter 567
1 paul 333
2 mary 23432

I now want to replace the certain strings from df1 with the coresponding 'name' of df2.
the result should look like this:

text
0 aaapeter
1 bbbpaul
2 cccmary

do you have any hints how to do that? I assume this could be done with 'iloc', but i am clueless ...

tnx for any help :)

🌐
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 - BUG: Pandas.DataFrame.str.replace function fails silently for mixed data mixing strings and float/int and replaces with NaN#45372 ... BugStringsString extension data type and string dataString extension data type and string datareplacereplace methodreplace method ... I have checked that this issue has not already been reported.
Author: pandas-dev
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › pandas dataframe replace( ) function not working
r/learnpython on Reddit: Pandas dataframe replace( ) function not working
April 30, 2022 -

Full code: https://gist.github.com/VictorLG98/0712146c957921b97bc1e5bf3f41c256

The code:

participants = []
        for puid in match_detail['metadata']['participants']:
            participants.append(self.watcher.summoner.by_puuid(self.my_region, puid)['name'])

        par = [i for i in participants]
        champ1 = [i['championName'] for i in match_detail['info']['participants']]
        role1 = [i['individualPosition'] for i in match_detail['info']['participants']]
        kills = [i['kills'] for i in match_detail['info']['participants']]
        deaths1 = [i['deaths'] for i in match_detail['info']['participants']]
        assists1 = [i['assists'] for i in match_detail['info']['participants']]
        wards1 = [i['wardsPlaced'] for i in match_detail['info']['participants']]
        gold = [i['goldEarned'] for i in match_detail['info']['participants']]
        minions = [i['totalMinionsKilled'] for i in match_detail['info']['participants']]
        neu_minions = [i['neutralMinionsKilled'] for i in match_detail['info']['participants']]
        suma1 = [x + y for x, y in zip(minions, neu_minions)]
        dano_total = [i['totalDamageDealtToChampions'] for i in match_detail['info']['participants']]
        dano_recibido = [i['totalDamageTaken'] for i in match_detail['info']['participants']]
        win = [i['win'] for i in match_detail['info']['participants']]

        data = {
            'Invocador': par,
            'Champion': champ1,
            'Role': role1,
            'Kills': kills,
            'Deaths': deaths1,
            'Assists': assists1,
            'Wards': wards1,
            'Gold Earned': gold,
            'Farm': suma1,
            'Daño total': dano_total,
            'Daño recibido': dano_recibido,
            'Win': win
        }
        df = pd.DataFrame(data)
        df['Role'].replace(to_replace=dict(UTILITY='SUPPORT'), inplace=True)
        df['Win'].replace({'True': 'VICTORY', 'False': 'DEFEAT'}, inplace=True)

The first replace is working fine but the last is not working, i don't know why.

Result

🌐
GitHub
github.com › pandas-dev › pandas › issues › 24804
str.replace('.','') should replace every character? · Issue #24804 · pandas-dev/pandas
January 16, 2019 - import pandas as pd s = pd.Series(['abc','123']) s.str.replace('.','',regex = True) Out [1]: 0 abc 1 123 dtype: object · Hi everyone, I was showing str.replace to a colleague and how it uses regex by default and when I entered pd.Series.str.replace('.','') I expected every character to be removed but instead nothing happened.
Author: pandas-dev
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.6 documentation
Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used.
🌐
HAR Data Extractor
jonathansoma.com › course › foundations-2021 › replacing-with-str-replace-and-replace
Replacing with .str.replace and .replace
If you try to do that with .str.replace, you get an error: replace() missing 1 required positional argument: 'repl'. This means "You didn't tell me what to replace with what," even though it feels like you tried. # This will not work df['edited'] = df.original.str.replace({ "potatoes": "chocolate", "love": "hate" }) df
🌐
Reddit
reddit.com › r/learnpython › replace part of string in a column if string is at a certain position in pandas
r/learnpython on Reddit: Replace part of string in a column if string is at a certain position in Pandas
October 26, 2022 -

I have this sample column:

Reference
A3V345 1/2
763SDDY 2/2
645BRW0 1OF4
645BRW0 2OF4
GRYUGBM-A
67AQSD-B
EW21Z31
4GH5477BM 1/3

I would like to create a new column where the result is the same value in the reference column with "-B, -A, 2OF4, 1OF4, 2/2, 1/2" removed or replaced with ""; the desired output would be this:

new_value
A3V345
763SDDY
645BRW0
645BRW0
GRYUGBM
67AQSD
EW21Z31
4GH5477BM

So far I have attempted at least three different things with different error messages in the output:

1. str.endswith and concatenation of strings to identify position and somehow slice value after True value in cell

output_table_1["new_value"] = output_table_1["Reference"].str.endswith((" 1/2", " 2/2", " 1/3", "1OF4", "2OF4", "-A", "-B"), na = False)
output_table_1["concat"] = output_table_1["Reference"] + str(output_table_1["new_value"])
output_table_1

The output is a long text I didn't expect to see, for instance, for 4GH5477BM this is the result:

4GH5477BM 1/30 False\n1 False\n2 False\n3 False\n4 False\n ... 

Cell expanded: 4GH5477BM 1/30 False\n1 False\n2 False\n3 False\n4 False\n ... \n1014 False\n1015 False\n1016 False\n1017 True\n1018 True\nName: new_value, Length: 1019, dtype: bool

2. str.replace if condition (only one parameter as example) applies

if output_table_1[output_table_1["Reference"].str.endswith(" 1/2", na=False)]:
    output_table_1["new_value"] = output_table_1["Reference"].apply(lambda x: x.replace(" 1/2", ""))
else:
    output_table_1["new_value"] = output_table_1["Reference"]
Output:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

3. str.find to check position at the end of string

positionA = output_table_1["Reference"].str.len() - 4
positionB = output_table_1["Reference"].str.len()
output_table_1["set_value"] = output_table_1["Reference"].str.find(" 1/2", start = positionA, end = positionB)

The output is NaN for all the cells in that column, even though I believe it should return -1 if there was no match when searching the substring. Even if it worked, I still would be limited as it only accepts one string to search. As in the first try, I wanted to return the index where the occurrence happened and then use slicing to delete just before the searched substring.

I still lack skill in Python, more so in Pandas. Any help will be appreciated.

🌐
Reddit
reddit.com › r/learnpython › pandas df.replace replacing more than it should?
r/learnpython on Reddit: pandas df.replace replacing more than it should?
April 15, 2022 -

Essentially, I'm reading a csv file that has ' ' to indicate no value, which I want to turn into NaN. The problem is, it works exactly as expected on a minimum reproducable, but not on the real data and I don't understand why.

Minimum reproducable:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Pers_Nr': ['01', ' ', '03'], 
                'Name': ['George', 'Joe', ' ']})

print(df)

new_df = df.replace(' ', np.nan, regex=True)

print(new_df)

output as expected:

  Pers_Nr    Name
0      01  George
1             Joe
2      03

  Pers_Nr    Name
0      01  George
1     NaN     Joe
2      03     NaN

But then on real data, the same line of code produces this:

  Pers_Nr                       Name St_Kl Faktor AN 
0   00001             Youssef. R     1             0                                      
1   00002    G. Frederik Leonard                   4                                      
2   00003             O. Dietmar                   4                                                                                                                                                    

  Pers_Nr  Name St_Kl  Faktor AN 
0   00001   NaN     1     NaN  0           
1   00002   NaN   NaN     NaN  4           
2   00003   NaN   NaN     NaN  4

and I just don't get it. So thanks so much for any help! let me know if you need more info

🌐
GitHub
github.com › pandas-dev › pandas › issues › 34993
BUG: replace method with regex=True does not work for byte string · Issue #34993 · pandas-dev/pandas
June 25, 2020 - It worked in pandas 0.25. It also works when the data is unicode string instead of byte string. I see a discussion about regex parameter in #33302 and one suggestion is to use str.replace instead.
Author: pandas-dev
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › how to replace string in pandas dataframe
How to Replace String in Pandas DataFrame - Spark By {Examples}
June 13, 2025 - In pandas, to replace a string in the DataFrame column, you can use either the replace() function or the str.replace() method along with lambda methods.