3
$\begingroup$

I have data in pandas as below:

123-543-2345
876|678|3469
304-762-2467     

Trying to change all to this format: 123-543-2345

I tried the different code below:

df["Phone_Number"].str.replace('[^a-zA-Z0-9]','') 
#df['Phone_Number'].apply(lambda x: x[0.3] + '_' + x[3.6] + '_' + x[6.10])
df["Phone_Number"].apply(lambda x: str(x))

Yet still returning the same data without modification.

$\endgroup$
3
  • $\begingroup$ .str.replace() requires regex=True to be specified when you supply a regex expression. $\endgroup$ Commented Oct 14 at 14:12
  • $\begingroup$ Did you read the docs? .str.replace() doesn't use regex by default. That changed in Pandas 2.0. $\endgroup$ Commented Oct 14 at 19:57
  • $\begingroup$ Is the only problem the format of the middle example, or there are (known/expected/suspected) other formats in the full dataset? $\endgroup$ Commented Oct 14 at 21:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.