0

I have multi-column dataframe of Flickr tags with 41,000 rows. I want to remove multiple white spaces element in a column, leaving the other columns intact.

so, this is how my column looks like:

column1                            column2                                              column3
<a href="www.asia.com>Breda</a>    result    thisIs    Somany     multiplespace   s     25,000

and this is how I want my column2 looks like after I remove multiple white spaces in it:

column1                            column2                                 column3
<a href="www.asia.com>Breda</a>    result thisIs Somany multiplespace s    25,000

2 Answers2

0
df['column2'] = df['column2'].replace('\s+', ' ', regex=True)

will do your job, s\+ stands for more then one whitspace.

You can use this question to get another idea of it how it works, the command replace works with it.

PV8
  • 5,799
  • 7
  • 43
  • 87
0

it also works using function like this code below. it is a good functino to use to different columns.

 def replaceWhiteSpace(text):
    res = []
    for i in text:
        res = text.str.split()
    res = res.str.join(' ')
    return res

df['column2'] = replaceWhiteSpace(df['column2'])