How to remove unwanted spaces from a cell using pandas?

I have the below mentioned data.

,name,link,address
0,Aasia Steel Industrial Group,http://www.aasiasteel.com/,"
Address

                                        1 
                                                Saudi Arabia 
                                    "
1,ADES,http://investors.adihgroup.com/,"
Address

                                        Al-Kifah Tower 
                                                King Fahad Road 
                                                    Dhahran 
                                                Saudi Arabia 
                                    "
2,AEC,https://www.aecl.com,"
Address

                                        King Khalid International Airport. 
                                                Industrial Estate P.O.Box 90916, 
                                                    Riyadh 11623, 
                                                    Saudi Arabia 
                                    "

There is a lot of unwanted spaces. I tries using the below functions but I am not able to clean my cells.

df['address']=df.address.str.strip()

In the console the the output is address column follows:

\nAddress\r\n\r\n\t\t\t\t\t\t\t\t\t\tAl-Kifah

>Solution :

df['address'].apply(lambda x:' '.join(x.split()))

If the column has other variable excpet string then we can use:

df['address'].apply(lambda x:' '.join(x.split()) if hasattr(x,'lower') else x)

Leave a Reply