Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除列中大于1的所有空格_Python_Pandas_Whitespace - Fatal编程技术网

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+', ' ')