6

I've got a pandas DataFrame that uses "2Nd" instead of "2nd", "136Th" instead of "136th", etc. I want the letter immediately following the number to be lowercase.

Sample data:

data = pd.Series(['21St StNew York', 'Exampe BlvdSt Louis', '1St Rd'])

Desired output:

['21st StNew York', 'Exampe BlvdSt Louis', '1st Rd']

Tried using str.replace():

data = data.str.replace('\BSt', 'st', regex=True)
['21st StNew York', 'Exampe Blvdst Louis', '1st Rd']

Is it possible to use a capture group?

data = data.str.replace('[0-9]+(St)', 'st', regex=True)
['st StNew York', 'Exampe BlvdSt Louis', 'st Rd']

2 Answers 2

5

Use a callable for repl

new_data = data.str.replace('(\d+[A-Z])', lambda m: m.group(1).lower())

Out[49]:
0        21st StNew York
1    Exampe BlvdSt Louis
2                 1st Rd
dtype: object
Sign up to request clarification or add additional context in comments.

Comments

0

We can try doing a regex replacement on the pattern (?<=\d)[A-Z], and then replacing with the lowercase version:

df['dat'] = df['data'].str.replace(r'(?<=\d)[A-Z]', lambda x: x.group(0).lower())

1 Comment

This is returning null values for me for some reason.

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.