27

How do I remove multiple spaces between two strings in python.

e.g:-

"Bertug 'here multiple blanks' Mete" => "Bertug        Mete"

to

"Bertug Mete" 

Input is read from an .xls file. I have tried using split() but it doesn't seem to work as expected.

import pandas as pd , string , re

dataFrame = pd.read_excel("C:\\Users\\Bertug\\Desktop\\example.xlsx")

#names1 =  ''.join(dataFrame.Name.to_string().split()) 

print(type(dataFrame.Name))

#print(dataFrame.Name.str.split())

Let me know where I'm doing wrong.

2
  • whats problem whit this: names1 = ' '.join(dataFrame.Name.to_string().split()) Mar 28, 2017 at 13:54
  • @Bertug please add example how your data is looks like. Mar 28, 2017 at 13:54

1 Answer 1

52

I think use replace:

df.Name = df.Name.replace(r'\s+', ' ', regex=True)

Sample:

df = pd.DataFrame({'Name':['Bertug     Mete','a','Joe    Black']})
print (df)
              Name
0  Bertug     Mete
1                a
2     Joe    Black

df.Name = df.Name.replace(r'\s+', ' ', regex=True)
#similar solution
#df.Name = df.Name.str.replace(r'\s+', ' ')
print (df)
          Name
0  Bertug Mete
1            a
2    Joe Black
4
  • I'm using Python 3 and this didn't work for me. Perhaps the solution is Python 2. This solution worked for me stackoverflow.com/a/5658439/2763005
    – Allan Tsai
    Feb 1, 2020 at 13:22
  • 5
    Using Python 3, df["Name"].str.replace('\s+', ' ', regex=True) worked. Notice the additional str.
    – d_gnz
    Aug 22, 2020 at 22:03
  • remember to add an r before the regex if working with pandas in python 3r'\s+' Mar 25, 2022 at 7:13
  • use df.columns = df.columns.str.replace(r'\s+', ' ', regex=True) to strip duplicate white spaces in all the columns.
    – Akhil S
    Mar 8 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.