删除“熊猫”列中所有大于一的空格

时间:2019-10-22 18:21:24

标签: python pandas whitespace

我有这段适用于字符串的代码,但需要将其应用于pandas列。有什么想法吗?

foo = "hey     how are \n you doing today?"
foo = " ".join(foo.split())

输出:

print(foo)
hey how are you doing today?

如何将其应用于大熊猫中的整列文字?

2 个答案:

答案 0 :(得分:3)

您可以使用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

答案 1 :(得分:3)

str.replace是必经之路:

 df['col'] = df['col'].str.replace('\s+', ' ')