Python / Pandas - How to remove extra space between two words in a column of the data frame

0

The "strip" function removes the spaces on the left and right, but does not remove the spaces between the words:

raw_data = {'NAME': ['   José  Luiz   da   Silva   ']}
df = pd.DataFrame(raw_data, columns = ['NAME'])
df['NAME'] = df['NAME'].str.strip()
    
asked by anonymous 04.07.2017 / 16:16

1 answer

2
import pandas as pd
rdata = {'NAME': ['   José           Luiz   da       Silva   ']}
df = pd.DataFrame(rdata, columns = ['NAME'])

for i, row in df.iterrows():
  df.set_value(i,'NAME',' '.join(row.NAME.split()))

print (df)  

Output:

                 NAME
0  José Luiz da Silva

Run on repl.it.

    
04.07.2017 / 21:05