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.
Series. str. strip()” to remove the whitespace from the string. Using strip function we can easily remove extra whitespace from leading and trailing whitespace from staring.
We can remove multiple spaces from a string with a single space by using the re. sub() method in Python which is imported from the re library.
lstrip() is used to remove spaces from the left side of string, str. rstrip() to remove spaces from right side of the string and str. strip() removes spaces from both sides. Since these are pandas function with same name as Python's default functions, .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With