Python 删除列中大于1的所有空格
我有一段适用于字符串的代码,但需要将其应用于列。有什么想法吗Python 删除列中大于1的所有空格,python,pandas,whitespace,Python,Pandas,Whitespace,我有一段适用于字符串的代码,但需要将其应用于列。有什么想法吗 foo = "hey how are \n you doing today?" foo = " ".join(foo.split()) 输出: print(foo) hey how are you doing today? 如何在pandas中的整列文本中应用此选项?您可以使用str.replace df = pd.DataFrame({'col':['how are you?', 'This is a good e
foo = "hey how are \n you doing today?"
foo = " ".join(foo.split())
输出:
print(foo)
hey how are you doing today?
如何在pandas中的整列文本中应用此选项?您可以使用
str.replace
df = pd.DataFrame({'col':['how are you?', 'This is a good example']})
df['col'] = df['col'].str.replace('\s{2,}', ' ')
col
0 how are you?
1 This is a good example
您可以使用
str.replace
df = pd.DataFrame({'col':['how are you?', 'This is a good example']})
df['col'] = df['col'].str.replace('\s{2,}', ' ')
col
0 how are you?
1 This is a good example
str.replace
是一种方法:
df['col'] = df['col'].str.replace('\s+', ' ')
str.replace
是一种方法:
df['col'] = df['col'].str.replace('\s+', ' ')