Replace All Occurrences of a String in a Pandas Dataframe (Python)

Replace all occurrences of a string in a pandas dataframe (Python)

You can use replace and pass the strings to find/replace as dictionary keys/items:

df.replace({'\n': '<br>'}, regex=True)

For example:

>>> df = pd.DataFrame({'a': ['1\n', '2\n', '3'], 'b': ['4\n', '5', '6\n']})
>>> df
a b
0 1\n 4\n
1 2\n 5
2 3 6\n

>>> df.replace({'\n': '<br>'}, regex=True)
a b
0 1<br> 4<br>
1 2<br> 5
2 3 6<br>

Note that this method returns a new DataFrame instance by default (it does not modify the original), so you'll need to either reassign the output:

df = df.replace({'\n': '<br>'}, regex=True)

or specify inplace=True:

df.replace({'\n': '<br>'}, regex=True, inplace=True)

How do I replace all the instances of a certain character in a dataframe?

Consider the dataframe df

df = pd.DataFrame([['?', 1], [2, '?']])

print(df)

0 1
0 ? 1
1 2 ?

replace

df.replace('?', 0)

0 1
0 0 1
1 2 0

mask or where

df.mask(df == '?', 0)
# df.where(df != '?', 0)

0 1
0 0 1
1 2 0

However, imagine your dataframe has ? within longer strings.

df = pd.DataFrame([['a?', 1], [2, '?b']])

print(df)

0 1
0 a? 1
1 2 ?b

replace with regex=True

df.replace('\?', '0', regex=True)

0 1
0 a0 1
1 2 0b

Pandas: Search and replace a string across all columns in a dataframe

Try this

df = pd.DataFrame({'A': ['None', 'NONE', '565'], 
'B': ['234', 'NONE', '347']})
# replace NONE by None
df = df.replace('NONE', 'None')
print(df)
      A     B
0 None 234
1 None None
2 565 347

Is there a way to replace all substrings in a pandas dataframe?

Try:

df = df.astype(str).replace("\[xReturn\]", "", regex=True)

How to replace text in a string column of a Pandas dataframe?

Use the vectorised str method replace:

df['range'] = df['range'].str.replace(',','-')

df
range
0 (2-30)
1 (50-290)

EDIT: so if we look at what you tried and why it didn't work:

df['range'].replace(',','-',inplace=True)

from the docs we see this description:

str or regex: str: string exactly matching to_replace will be replaced
with value

So because the str values do not match, no replacement occurs, compare with the following:

df = pd.DataFrame({'range':['(2,30)',',']})
df['range'].replace(',','-', inplace=True)

df['range']

0 (2,30)
1 -
Name: range, dtype: object

here we get an exact match on the second row and the replacement occurs.

Pandas: replace substring in string

Use replace with dict for replacing and regex=True:

df['url'] = df['url'].replace({'icashier.alipay.com': 'aliexpress.com'}, regex=True)
print (df)
url
0 aliexpress.com/catalog/2758186/detail.aspx
1 aliexpress.com/catalog/2758186/detail.aspx
2 aliexpress.com/catalog/2758186/detail.aspx
3 vk.com

How to replace exact string to other using replace() of Panda.DataFrame?

Try:

df['tumor-size'] = df['tumor-size'].replace("^'0-4'$", "'00-04'")

replace part of an int or string in a pandas dataframe column upon condition

You can use .str.replace with regex

df['New_date'] = df['Original_date'].astype(str).str.replace('(\d{4})(13|14)(\d{2})', r'\g<1>12\3', regex=True)
print(df)

Original_date New_date
0 20190101 20190101
1 20191301 20191201
2 20191401 20191201


Related Topics



Leave a reply



Submit