-3

Here's my dataset

Id          Two Words
1             jawa timur
2           jawa  barat
3           kalimantan selatan

What I want is only one space between two words, no space otherwise

Id          Two Words
1           jawa timur
2           jawa barat
3           kalimantan selatan
Nabih Bawazir
  • 5,275
  • 6
  • 29
  • 53

1 Answers1

0
df["Two Words"] = df["Two Words"].str..strip().replace(' +', ' ',regex=True)
Ravindra S
  • 5,906
  • 12
  • 67
  • 103
  • 2
    should also strip first: `df['Two Words'] = df['Two Words'].str.strip().str.replace(r' +', r' ', regex=True)` – tdy Jun 03 '22 at 01:22