How can I replace values in a column with a dictionary?

Hi, I would like to replace every elements from a column in a Julia Dataframe using a dictionary.
I found the function “replace” but didn’t understand how it use.

Here is probably the most robust way of doing it

julia> df = DataFrame(a = [1, 2], b = [3, 4])
2×2 DataFrame
 Row │ a      b
     │ Int64  Int64
─────┼──────────────
   1 │     1      3
   2 │     2      4

julia> replace(t -> get(replacement_dict, t, missing), df.a)
2-element Vector{Int64}:
 100
 200

julia> df.a = replace(t -> get(replacement_dict, t, missing), df.a)

You can also do replace(df.a, replacement_dict...), but that will cause problems with a very large Dict.

1 Like

Or broadcasting instead of an anonymous function

get.(Ref(replacement_dict), df.a, missing)

Also I think you can avoid allocations if you do df.a .= get.(..), although with missing as third argument to get that will fail if some elements in your column are not in the dict and your column isn’t a Union{T, Missing} column.

This is even easier using @rtransform in DataFramesMeta.jl

@rtransform df :a2 = get(replacement_dict, :a, missing)