How to remove special character like  using Python?
In this blog post I am going to share you an Python function which can be used to remove special character in your data. It is normal that you will get special character like Ä, NÄ�sÄ«f etc when scrape from website. This will cause trouble when you want to export the data and it also doesn't looks good. Below code helps us to solve the problem:
def cleanup(value):
return value.encode('ascii', errors='replace').replace("?"," ")
You can run the above funtion in Pandas to column which got the special character. Below is one sample:
data['Name']= data['Name'].apply(cleanup)
In above line data is the data frame and it has column named as Name. I am using apply function to run my custom code which will replace the any weird character to blanks.
You can covert any column to numeric using below line:
data['Age'] = data['Age'].apply(pd.to_numeric, errors='coerce')
def cleanup(value):
return value.encode('ascii', errors='replace').replace("?"," ")
You can run the above funtion in Pandas to column which got the special character. Below is one sample:
data['Name']= data['Name'].apply(cleanup)
In above line data is the data frame and it has column named as Name. I am using apply function to run my custom code which will replace the any weird character to blanks.
You can covert any column to numeric using below line:
data['Age'] = data['Age'].apply(pd.to_numeric, errors='coerce')
Again in above line I am using apply function but this time I am calling a Pandas function instead of my custom function.
Comments
Post a Comment