我有这段适用于字符串的代码,但需要将其应用于pandas列。有什么想法吗?
foo = "hey how are \n you doing today?"
foo = " ".join(foo.split())
输出:
print(foo)
hey how are you doing today?
如何将其应用于大熊猫中的整列文字?
答案 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+', ' ')