4

The simple dataframe replace shown below is not working.
The NewPhone column contains the same value as the original column.

import pandas as pd
SF = pd.read_csv(r"xxx.csv")
SF['NewPhone'] = SF['Phone'].replace("(",'xxx')
print(SF['NewPhone'])
0

2 Answers 2

13

replace looks for exact matches (by default unless you pass regex=True but you will need to escape the parentheses - see @piRSquared's answer), you want str.replace:

SF['NewPhone'] = SF['Phone'].str.replace("(",'xxx')

which will replace all occurrences of the passed in string with the new string

Example:

In[20]:
df = pd.DataFrame({'phone':['(999)-63266654']})
df

Out[20]: 
            phone
0  (999)-63266654

In[21]:    
df['phone'].str.replace("(",'xxx')

Out[21]: 
0    xxx999)-63266654
Name: phone, dtype: object

If we try replace then no match occurs:

In[22]:
df['phone'].replace("(",'xxx')

Out[22]: 
0    (999)-63266654
Name: phone, dtype: object

See @piRSquared's answer for how to get replace to work as expected (I don't want to cannibalise his answer)

Sign up to request clarification or add additional context in comments.

Comments

5

The Series.replace method takes a regex argument that is False by default. Set it to True. Also, if the string to be replaced is interpreted as a regex pattern, we'll need to escape the opening parenthesis.

df.phone.replace("\(", 'xxx', regex=True)

0    xxx999)-63266654
Name: phone, dtype: object

Setup from @EdChum

df = pd.DataFrame({'phone':['(999)-63266654']})

1 Comment

I still think I prefer your answer. But it's good to remind people of this option. It's also available for the DataFrame variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.