Skip to content
Advertisement

Regexp_replace “,” with “.” every other commas in spark

I have a dataframe that instead of . it has , and separators of numbers are also comma, I need to replace only odd comma to dot. The dataframe is very big but as an example, I have this:

JavaScript

I want this df:

JavaScript

Advertisement

Answer

You can split on all commas , and later you can use for-loop:

  1. with range(0, len(splitted_data), 2) to create pairs [0:2], [2:4], …, [n:n+2] and join them to strings with dots:
JavaScript
  1. with iter() with zip() to create pairs and join them to strings with dots:
JavaScript

Result:

JavaScript

EDIT:

You may also use regex for this

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement