Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 690
A Rob4 Avatar asked May 20 '17 11:05

A Rob4


People also ask

How do you change from double space to single space in Python?

Use the re. sub() method to replace multiple spaces with a single space, e.g. result = re. sub(' +', ' ', my_str) .

How do I remove a space in pandas?

Pandas provide predefine method “pandas. Series. str. strip()” to remove the whitespace from the string.

How do I replace a specific value in a column in pandas?

replace() function is used to replace values in column (one value with another value on all columns). This method takes to_replace, value, inplace, limit, regex and method as parameters and returns a new DataFrame. When inplace=True is used, it replaces on existing DataFrame object and returns None value.


2 Answers

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

df.postcode = df.postcode.str.replace('  ', ' ')
like image 149
Ami Tavory Avatar answered Oct 22 '22 01:10

Ami Tavory


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()
like image 23
leo Avatar answered Oct 22 '22 00:10

leo