Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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['NewPhone'] = SF['Phone'].replace("(",'xxx')
print(SF['NewPhone'])
like image 584
Jerry Avatar asked Dec 03 '22 20:12

Jerry


1 Answers

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)

like image 148
EdChum Avatar answered Dec 29 '22 18:12

EdChum