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

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

#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)

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

2 Likes

avoiding “…”

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

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.

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.

A couple of other options were discussed recently here

1 Like