# Does Julia have equivalent of Python: DF.column.map( dict )

**URL:** <https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 22, 2020, 6:08am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048 "2020-09-22T06:08:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 22, 2020, 6:08am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/1 "2020-09-22T06:08:54Z")

</div>

The best I could find is map( x → dict , DF.column )

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 22, 2020, 6:21am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/2 "2020-09-22T06:21:40Z")

</div>

Your solution is perfectly fine.  
The main reason that Python needs an explicit function for this is performance - Pandas map on a Dictionary is much faster than e.g. applying an anonymous Python function.  
As an alternative to an anonymous function you could use broadcasting:

```julia
getindex.(Ref(dict), DF.column)

```

The `Ref` indicates here that broadcasting is not done for the dictionary, but only for the array (2nd argument).

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 22, 2020, 6:31am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/3 "2020-09-22T06:31:46Z")

</div>

Thanks lungben. I didn’t know about the Ref syntax . Thats useful.

Dictionary lookup cant be directly broadast ie. dict.[DF.column]  
doesnt work.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 22, 2020, 7:02am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/4 "2020-09-22T07:02:01Z")

</div>

Unfortunately not, but I agree that it would be nice to have.  
Maybe it is worth filing an issue to introduce a syntax like this?

```julia
d.[A] 
# should be equivalent to
getindex.(Ref(d), A) 

```

Or is there a fundamental reason against it?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 22, 2020, 7:05am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/5 "2020-09-22T07:05:12Z")

</div>

That would be great.

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [September 22, 2020, 8:38am UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/6 "2020-09-22T08:38:09Z")

</div>

> [@lungben](#):
>
> Or is there a fundamental reason against it?

The experts may chime in soon with a concise explanation, but in the meantime if you’d like to dig into the reasoning that led to the current design, check out issues [#18618](https://github.com/JuliaLang/julia/issues/18618) and [#25904](https://github.com/JuliaLang/julia/issues/25904).

---

<div class="post-metadata">

**Author:** ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)\
**Post date:** [February 28, 2021, 3:30pm UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/7 "2021-02-28T15:30:08Z")

</div>

for me at least using ggplot2’s mpg dataset

```nohighlight
idx_map = Dict(key => idx for (idx, key) in enumerate(unique(df.class)))

julia> @time map(x -> idx_map[x], df.class)
  0.060553 seconds (124.05 k allocations: 8.437 MiB, 99.45% compilation time)

julia> @time getindex.(Ref(idx_map), df.class)
  0.000025 seconds (4 allocations: 2.062 KiB)

```

Huge win for `getindex.`!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 28, 2021, 5:54pm UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/8 "2021-02-28T17:54:55Z")

</div>

> [@Storopoli](#):
>
> Huge win for `getindex.` !

Maybe. But `@time` is not suitable for microbenchmarks. Can you try the same using BenchmarkTools? And remember variable interpolation.

---

<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:** [February 28, 2021, 6:55pm UTC](https://discourse.julialang.org/t/does-julia-have-equivalent-of-python-df-column-map-dict/47048/9 "2021-02-28T18:55:14Z")

</div>

This is a compilation time thing. Creating a new anonymous function has a fixed compilation cost, so `map(t -> ..., x)` is slow

```julia
julia> x = rand(1:5, 100);

julia> d = Dict(1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E");

julia> @time getindex.(Ref(d), x);
  0.144137 seconds (172.92 k allocations: 9.074 MiB, 42.83% gc time)

julia> @time getindex.(Ref(d), x);
  0.000016 seconds (4 allocations: 976 bytes)

julia> @time map(xi -> d[xi], x);
  0.081783 seconds (98.49 k allocations: 5.256 MiB)

julia> @time map(xi -> d[xi], x);
  0.078627 seconds (55.19 k allocations: 2.922 MiB)

julia> get_from_d = let d = d
       xi -> d[xi]
       end;

julia> @time map(get_from_d, x);
  0.043167 seconds (43.72 k allocations: 2.272 MiB)

julia> @time map(get_from_d, x);
  0.000024 seconds (2 allocations: 928 bytes)

```

This re-compilation problem only shows up in global scope, though.

```julia
julia> function get_from_d_wrapper(d, x)
       map(xi -> d[xi], x)
       end;

julia> @time get_from_d_wrapper(d, x);
  0.037174 seconds (46.42 k allocations: 2.426 MiB)

julia> @time get_from_d_wrapper(d, x);
  0.000006 seconds (1 allocation: 896 bytes)

```
