Pandas Dataframe Replace not working for sub-sentence

Related searches

I have a dataframe with a column A which contains values of the form:

Col A

this is to be replaced
nonsense, this is to be replaced
nonsense
garbage
this is to be replace, nonsense

Ideal output:

Col A

this has been replaced
nonsense, this has been replaced
nonsense
garbage
this has been replaced, nonsense

I have tried:

df['Col A'].replace('this is to be replaced', 'this has been replaced')
df['Col A'].str.replace('this is to be replaced', 'this has been replaced', regex=True, inplace=True)
df['Col A'].replace({'this is to be replaced':'this has been replaced'}, regex=True, inplace=True)
df['Col A'].replace(regex= ['this is to be replaced'], value= 'this has been replaced')

Basically all standard ways to solve this. The issue seems to be the spaces in the substring. When I try to replace a specific word, it works fine.

Any ideas?

EDIT: I tried all of y'alls suggestions, they do not work. As additional context:

The exact string to be replaced is:

MATHEMATICS (Math 1601 & 1602)

to

MATHEMATICS (Math 1601 & Math 1602)

I have also tried:

df['col A'] = df['col A'].replace('1602', 'Math 1602')

The problem is you are missing to set the result to your dataframe's column.

You can use below code to achieve your goal:

import pandas as pd
l = ["this is to be replaced","nonsense, this is to be replaced","nonsense","garbage","this is to be replace, nonsense"]
df = pd.DataFrame(l,columns=["Col A"])
df["Col A"] = df["Col A"].str.replace("is to be","has been")

Then the new DataFrame looks like:

>>> df
                              Col A
0            this has been replaced
1  nonsense, this has been replaced
2                          nonsense
3                           garbage
4   this has been replace, nonsense

pandas.Series.str.replace — pandas 1.1.1 documentation, sub() , depending on the regex value. Parameters. patstr or compiled regex. String can be a character sequence or regular expression. replstr� to_replace : [str, regex, list, dict, Series, numeric, or None] pattern that we are trying to replace in dataframe. value : Value to use to fill holes (e.g. 0), alternately a dict of values specifying which value to use for each column (columns not in the dict will not be filled).

Is this what you are looking for?

df = pd.DataFrame({'Col A':
['this is to be replaced',
'nonsense, this is to be replaced',
'nonsense',
'garbage',
'this is to be replace, nonsense']})
df.replace(to_replace=['is to be'], value = 'has been', regex = True, inplace = True)
df

Python, The image of data frame before any operations is attached below. Example #1: Replacing values in age column. In this example, all the values� See the examples section for examples of each of these. value scalar, dict, list, str, regex, default None. Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled).

You can simply use replace passing the correct parameters:

data = {'index':[1,2,3,4,5],'Col A':['this is to be replaced','nonsense, this is to be replaced','nonsense','garbage','this is to be replaced']}
df = pd.DataFrame(data)
print(df)
df['Col A'].replace('is to be','has been',regex=True,inplace=True)
print(df)

Output:

  index                             Col A
0      1            this has been replaced
1      2  nonsense, this has been replaced
2      3                          nonsense
3      4                           garbage
4      5            this has been replaced

Replace values in Pandas dataframe using regex, In this post, we will use regular expressions to replace strings which have some pattern to it. Problem #1 : You are given a dataframe which� Values considered “missing”¶ As data comes in many shapes and forms, pandas aims to be flexible with regard to handling missing data. While NaN is the default missing value marker for reasons of computational speed and convenience, we need to be able to easily detect this value with data of different types: floating point, integer, boolean, and general object.

Replace strings in Python (replace, translate, re.sub, re.subn), This method is safe if you do not know what newline characters are included. print(s_lines_multi.splitlines()) # ['one', 'two� Parameters buf str, Path or StringIO-like, optional, default None. Buffer to write to. If None, the output is returned as a string. columns list of label, optional. The subset of columns to write.

Replacing Multiple Patterns in a Single Pass, Replacing Multiple Patterns in a Single Pass Credit: Xavier Defrang Problem You In particular, the sub method of re objects makes regular expressions a good� I'm getting the same issue in my Python Jupyter Notebook while trying to write a Pandas Dataframe to Snowflake. I can confirm that i have all the rights/access since i'm connecting as SYSADMIN role. In fact i can successfully drop and re-create the tables; I can confirm that the if_exists="append" option works just fine

One way of renaming the columns in a Pandas dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.

Comments
  • This worked! Although I am pretty sure I tried this earlier. I changed the words to be replaced with a more condensed version and it worked. Thank you!