本文介绍了删除DataFrame中的多个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在python中删除两个字符串之间的多个空格.
How do I remove multiple spaces between two strings in python.
例如:-
"Bertug 'here multiple blanks' Mete" => "Bertug Mete"
到
"Bertug Mete"
从.xls文件读取输入.我试过使用split(),但它似乎没有按预期工作.
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.
推荐答案
我认为使用replace
:
df.Name = df.Name.replace('\s+', ' ', regex=True)
示例:
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('\s+', ' ', regex=True)
#similar solution
#df.Name = df.Name.str.replace('\s+', ' ')
print (df)
Name
0 Bertug Mete
1 a
2 Joe Black
这篇关于删除DataFrame中的多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!