llssff
llssff

Reputation: 119

Pandas remove space after regex

One of my columns currently has an extra space after a colon.

ex: 11-july-2011 11: 30:30

Is there a way to remove the space without removing the first one as well?

Upvotes: 1

Views: 232

Answers (3)

FObersteiner
FObersteiner

Reputation: 25564

different approach: you can parse to datetime - and strftime if you like, e.g.

import pandas as pd
t = pd.to_datetime("11-july-2011 11: 30:30", format='%d-%B-%Y %H: %M:%S')

t.isoformat()
# '2011-07-11T11:30:30'

t.strftime('%B %d %Y, %H:%M:%S')
# 'July 11 2011, 11:30:30'

Upvotes: 0

vbid
vbid

Reputation: 159

You can replace the Spaces using .replace(':\s+', ':') , this piece of code searches for a colon followed by a space and replaces it with a colon , so you basically just remove the space which is behind a colon :)

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150765

You can search for spaces which follow : and replace them:

df['col_name'] = df['col_name'].str.replace(':\s+', ':')

Upvotes: 4

Related Questions