Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to understand this lambda with 3 .replace() line of code

df['Current Ver'] = df['Current Ver'].astype(str).apply(lambda x : x.replace('.', ',',1).replace('.', '').replace(',', '.',1)).astype(float)

Slowly learning lambda command, my understanding of this line of code is:

  1. Change dataframe type to str
  2. Apply lambda with one perimeter x
  3. Replace all the string format . to , , (I don’t understand what does 1 stands for, have done research prior asking, didn’t find clue)
  4. Replace all the string format . to null value
  5. Replace all the string format , to . , (again still have no clue what does 1 stands for in this case)
  6. Change dataframe type to float

Please help me better understanding this line of code, thank you

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This replaces the first . in the string with a ,, removes the remaining periods, changes the first , back to a ., then converts the result to a float for the 'Current Ver' column in the dataframe.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading