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'])
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With