A Rob4
A Rob4

Reputation: 1488

Remove double space and replace with a single one in pandas

I have 2m lines of Uk postcode data but some muppet has used double spaces in some cases and single spaces in others. I need to merge data based on the postcode so it needs to be consistent.

I can't find a simple way to do this in pandas, but it feels like there should be. Any advice?

Upvotes: 11

Views: 11282

Answers (3)

Fady Noshy
Fady Noshy

Reputation: 11

This should replace any kind of spces,tabs,..etc to one space:

df.postcode = df.postcode.str.replace('\s+', ' ')

Upvotes: 1

leo
leo

Reputation: 441

this should replace all multiple spaces with a single space

df.postcode = df.postcode.str.replace(' +', ' ')

remove all spaces from the start and end

df.postcode = df.postcode.str.strip()

Upvotes: 8

Ami Tavory
Ami Tavory

Reputation: 76366

You might be looking for pd.Series.str.replace:

df.postcode = df.postcode.str.replace('  ', ' ')

Upvotes: 16

Related Questions