Replace A Character In A Python Dataframe Column
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['Ne
Solution 1:
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)-63266654Name: phone, dtype: object
See @piRSquared's answer for how to get replace
to work as expected (I don't want to cannibalise his answer)
Solution 2:
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']})
Post a Comment for "Replace A Character In A Python Dataframe Column"