2

I have this code that works for a string, but need to apply it to a pandas column. Any ideas?

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

output:

print(foo)
hey how are you doing today?

How to I apply this on a whole column of text within pandas?

codingInMyBasement
  • 728
  • 1
  • 6
  • 20

2 Answers2

4

You can use str.replace

df = pd.DataFrame({'col':['how  are you?', 'This is a good   example']})
df['col'] = df['col'].str.replace('\s{2,}', ' ', regex=True)


    col
0   how are you?
1   This is a good example
congusbongus
  • 13,359
  • 7
  • 71
  • 99
Vaishali
  • 37,545
  • 5
  • 58
  • 86
4

str.replace is the way to go:

 df['col'] = df['col'].str.replace('\s+', ' ', regex=True)
congusbongus
  • 13,359
  • 7
  • 71
  • 99
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74