# How to df\[:newConcatenatedCol\] = df\[:stringCol\] \* string(df\[:intCol\])?

**URL:** <https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207>\
**Category:** Data\
**Tags:** question\
**Created:** [February 21, 2017, 10:00am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207 "2017-02-21T10:00:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 21, 2017, 10:00am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/1 "2017-02-21T10:00:41Z")

</div>

How to convert a column of integers (or floats) in a column of strings and concatenate it to other string column(s) ?  
e.g.:

```julia
df = DataFrame(a=["aa","ab","ac"],year=[2015,2016,2017])
df[:c] = df[:a] * " " * string(df[:year])

```

That doesn’t work (`string(df[:year])` returns `"[2015,2016,2017]"` and not `["2015","2016","2017"]`)  
From [here](https://groups.google.com/forum/#!topic/julia-users/uiv95QMHP08) I learned I can do:

```julia
df = DataFrame(a=["aa","ab","ac"],year=[2015,2016,2017])
df[:yearString] = [string(x) for x in df[:year]] 

```

but, aside it’s longer, still I have a MethodError when I then try to concatenate the columns:

```julia
df[:c] = df[:a] * " " * df[:yearString]

```

(bdw, `df[:c] = df[:a] * " "` alone works… )

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 21, 2017, 10:08am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/2 "2017-02-21T10:08:01Z")

</div>

Found it… to concatenate a column with a constant I need to use `*`, while to concatenate between columns I need to use the vectorised version `.*`:

```julia
df = DataFrame(a=["aa","ab","ac"],year=[2015,2016,2017])
df[:yearString] = [string(x) for x in df[:year]] 
df[:c] = df[:a] * " " .* df[:yearString] 

```

or

```julia
df = DataFrame(a=["aa","ab","ac"],year=[2015,2016,2017])
df[:c] = map((x,y) -> string(x, " ", y), df[:a], df[:year])

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 21, 2017, 10:12am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/3 "2017-02-21T10:12:47Z")

</div>

You should use `.*` in both cases since you want to operate element-wise.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 21, 2017, 10:17am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/4 "2017-02-21T10:17:13Z")

</div>

@nalimilan Actually I did try, but you get a MethodError when you use `df[:a] .* " "` :

```julia
df = DataFrame(a=["aa","ab","ac"],year=[2015,2016,2017])
df[:yearString] = [string(x) for x in df[:year]] 
df[:c] = df[:a] .* " " .* df[:yearString]

```

> MethodError: no method matching ._(::String, ::String)  
> Closest candidates are:  
> ._{T\<:AbstractString}(!Matched::Array{T\<:AbstractString,1}, ::AbstractString) at strings/basic.jl:85  
> ._{T\<:AbstractString}(::AbstractString, !Matched::Array{T\<:AbstractString,1}) at strings/basic.jl:86  
> ._(!Matched::DataArrays.DataArray{T,N}, ::AbstractString) at /home/lobianco/.julia/v0.5/DataArrays/src/operators.jl:240  
> …  
> in macro expansion at /home/lobianco/.julia/v0.5/DataArrays/src/operators.jl:244 [inlined]  
> in macro expansion at /home/lobianco/.julia/v0.5/DataArrays/src/utils.jl:17 [inlined]  
> in .\*(::DataArrays.DataArray{String,1}, ::String) at /home/lobianco/.julia/v0.5/DataArrays/src/operators.jl:242  
> in include\_string(::String, ::String) at ./loading.jl:441  
> in eval(::Module, ::Any) at ./boot.jl:234  
> in (::Atom.##67#70)() at /home/lobianco/.julia/v0.5/Atom/src/eval.jl:40  
> in withpath(::Atom.##67#70, ::Void) at /home/lobianco/.julia/v0.5/CodeTools/src/utils.jl:30  
> in withpath(::Function, ::Void) at /home/lobianco/.julia/v0.5/Atom/src/eval.jl:46  
> in macro expansion at /home/lobianco/.julia/v0.5/Atom/src/eval.jl:109 [inlined]  
> in (::Atom.##66#69)() at ./task.jl:60

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 21, 2017, 11:04am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/5 "2017-02-21T11:04:31Z")

</div>

Ah, sorry. That was a bug in DataArrays which seems to be fixed with latest master on Julia 0.6.

---

<div class="post-metadata">

**Author:** ![alasaadstat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alasaadstat/32/975_2.png) [@alasaadstat](https://discourse.julialang.org/u/alasaadstat)\
**Post date:** [May 2, 2017, 3:22am UTC](https://discourse.julialang.org/t/how-to-df-newconcatenatedcol-df-stringcol-string-df-intcol/2207/6 "2017-05-02T03:22:53Z")

</div>

You can try [DataFramesMeta.jl](https://github.com/JuliaStats/DataFramesMeta.jl).

```julia
using DataFramesMeta
@transform(df, c = mapslices(x -> join(x), [:a :year], [2]) |> vec)

```

You might as well check [Query.jl](http://www.david-anthoff.com/Query.jl/stable/).
