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

Time:12-03

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

CodePudding user response:

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.

  • Related