# Replace values in a DataFrame column based on a Dict of corresponding values

**URL:** <https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [June 3, 2021, 7:38pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353 "2021-06-03T19:38:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![franco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/franco/32/16905_2.png) [@franco](https://discourse.julialang.org/u/franco)\
**Post date:** [June 3, 2021, 7:38pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/1 "2021-06-03T19:38:40Z")

</div>

Is there a way to replace values in a DataFrame column based on a Dict() of corresponding values:

```julia
#Read the CSV file and transform it into a DataFrame
	rain_data = CSV.read("data/historico_precipitaciones.csv", DataFrame)
	#Rename the columns
	colnames = ["Year", "Month", "mm", "Days"]
	#Symbol is the type of object used to represent the labels of a dataset
	rename!(rain_data, Symbol.(colnames))
	#We use a dictionary to translate de Month names
	translate = Dict(
		"Enero" => "January",
		"Febrero" => "February",
		"Marzo" => "March",
		"Abril" => "April",
		"Mayo" => "May",
		"Junio" => "June",
		"Julio" => "July",
		"Agosto" => "August",
		"Septiembre" => "September",
		"Noviembre" => "November",
		"Diciembre" => "December"
	)
	for i in 1:length(rain_data[:,:Month])
    	rain_data[i,:Month] = translate[rain_data[i,:Month]]
	end

```

but instead of the for loop doing something like:  
`replace!(rain_data.Month, translate)`

---

<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:** [June 3, 2021, 7:40pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/2 "2021-06-03T19:40:43Z")

</div>

You can do `replace(x, translate...)`

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [June 3, 2021, 10:37pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/3 "2021-06-03T22:37:03Z")

</div>

avoiding “…”

```julia
fun123(x, dict) = get.(Ref(dict), x, missing)
x = fun123(x, translate)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 3, 2021, 10:44pm UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/4 "2021-06-03T22:44:51Z")

</div>

> [@DataFrames](#):
>
> avoiding “…”

are there performance issues associated with splat here? I worry about that too. But I think it’s only a concern if the dict is too large.

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [June 4, 2021, 1:23am UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/5 "2021-06-04T01:23:33Z")

</div>

You are right, it wouldn’t be a problem with this particular question. however, I like to record a solution when I think it is simple but more general.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [June 4, 2021, 1:42am UTC](https://discourse.julialang.org/t/replace-values-in-a-dataframe-column-based-on-a-dict-of-corresponding-values/62353/6 "2021-06-04T01:42:43Z")

</div>

A couple of other options were discussed recently here

> [@Transforming a DataFrame column by mapping with a Dict](https://discourse.julialang.org/t/transforming-a-dataframe-column-by-mapping-with-a-dict/61753):
>
> Let’s say I have a DataFrame and a Dict: df = DataFrame( a = 1:3, b = ["beetle", "fly", "spider"]) arthropod\_dict = Dict( "beetle" =\> "insect", "fly" =\> "insect", "spider" =\> "arachnid") And I’d like to apply the Dict like a map to the DataFrame, producing df = DataFrame( a = 1:3, b = ["beetle", "fly", "spider"], c = ["insect", "insect", "arachnid"]) Is there a way to do that using [transform](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.transform) or another DataFrame function so that I can apply the Dict within a [@…](https://github.com/jkrumbiegel/Chain.jl)
