2

I have a dataframe with a column (column 1) that looks like this. It's a bunch of numbers that are of type string. I would like to update "column 1" or create a new column "column 1 formatted" and the type to be either string or Float64 and to have numbers look like this instead.

|Column 1|    |Updated or new column|
|String  |    |Float64              |
----------    -----------------------
|400     |    |400.00               |
|120     |    |120.00               |
|37      |    |37.00                |
|1 500,00|    |1500.00              |
|166,14  |    |166.14               |
|13,95   |    |13.95                |
|1 148,18|    |1148.18              |

I have tried with replace to get rid of the "," and whitespace and parse it into Float64, but I haven't getting really any closer to what I want to achive.

hbrovell
  • 547
  • 6
  • 17

1 Answers1

3

Use:

df."Column 1 formatted" = parse.(Float64, replace.(df."Column 1", " "=>"", ','=>"."))
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • One question, using that code worked perfect for the numbers. But it created a new vector and not a new column in my dataframe. Is that correct or have I done something wrong? – hbrovell Sep 09 '22 at 14:42
  • 1
    The code creates `"Column 1 formatted"` column in your data frame. (or whatever name you gave it) – Bogumił Kamiński Sep 09 '22 at 14:54