Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace specific values in Julia Dataframe column with random value

I'm looking for a way to replace values in Dataframe column with random numbers. They should be different in every row where the substitution was performed.

For example replacing "X" with random numbers drawn from 100:120 range

julia> df = DataFrame(:a=>[1,2,"X","X",5,"X"],)
6×1 DataFrame
│ Row │ a   │
│     │ Any │
├─────┼─────┤
│ 1   │ 1   │
│ 2   │ 2   │
│ 3   │ X   │
│ 4   │ X   │
│ 5   │ 5   │
│ 6   │ X   │

* Replacing X with random values in 100:120 *

julia> df
6×1 DataFrame
│ Row │ a   │
│     │ Any │
├─────┼─────┤
│ 1   │ 1   │
│ 2   │ 2   │
│ 3   │ 103 │
│ 4   │ 110 │
│ 5   │ 5   │
│ 6   │ 116 │

I've tried using replace but rand is evaluated before replace:

julia> replace!(df.a,"X"=> rand(100:120))
julia> df
6×1 DataFrame
│ Row │ a   │
│     │ Any │
├─────┼─────┤
│ 1   │ 1   │
│ 2   │ 2   │
│ 3   │ 115 │
│ 4   │ 115 │
│ 5   │ 5   │
│ 6   │ 115 │
like image 309
Maciej Fender Avatar asked Feb 23 '26 03:02

Maciej Fender


1 Answers

A one liner could be:

replace!( x-> x=="X" ? rand() : x, df.a)
like image 85
Przemyslaw Szufel Avatar answered Feb 25 '26 23:02

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!