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

**URL:** <https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914>\
**Category:** General Usage\
**Tags:** dictionary, dataframes\
**Created:** [August 24, 2021, 2:48pm UTC](https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914 "2021-08-24T14:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [August 24, 2021, 2:48pm UTC](https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914/1 "2021-08-24T14:48:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 24, 2021, 2:54pm UTC](https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914/2 "2021-08-24T14:54:06Z")

</div>

Here is probably the most robust way of doing it

```julia
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`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 24, 2021, 4:29pm UTC](https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914/3 "2021-08-24T16:29:52Z")

</div>

Or broadcasting instead of an anonymous function

```julia
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.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 24, 2021, 4:53pm UTC](https://discourse.julialang.org/t/how-can-i-replace-values-in-a-column-with-a-dictionary/66914/4 "2021-08-24T16:53:51Z")

</div>

This is even easier using `@rtransform` in DataFramesMeta.jl

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

```
