Home > Mobile >  Transforming letters into 0 in pandas
Transforming letters into 0 in pandas

Time:02-17

I am a very beginner in Python Pandas. I have a Data set with wrongly types postal codes : last characters are random letters. How can I transform these letters into 0 ?

I tried this but obviously the whole postal code turns out to a 0 :

if data["CODE_POSTAL_PATIENT"].str.isalpha:

df1 = data["CODE_POSTAL_PATIENT"].transform(lambda x: 0)

Thanks in advance !

CodePudding user response:

Assuming you have zip codes like '12XY#' and want to change to '12000', use a regex to match the non digits and replace them with "0" using enter image description here

CodePudding user response:

Replace everything except digits

df['CODE_POSTAL'].str.replace('[^\d]','0',regex=True)
  • Related