# Passing DataFrameRow to transform in split-apply-combine

**URL:** https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [April 30, 2023, 10:53am UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118 "2023-04-30T10:53:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)
#### Post date: [April 30, 2023, 10:53am UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118/1 "2023-04-30T10:53:00Z")

</div>

Hello,

I want to apply a function (my\_func) to the DataFrameRow object, so I can access all the column-names from within the function, independently of the col\_names declared in the transform.  
I can’t find the way of accessing the Row object from within the transform function.

An example will clarify:

```julia
df = DataFrame(A=String[], B=String[])
push!(df, ["red","apple"])
push!(df, ["foo","bar"])

function unify_names(x::DataFrameRow)
       @unpack A,B = x
       return joinpath(A,B)
end

unify_names(df[1,:])
unify_names(df[2,:])

## I would like something like this...
transform(df, :All=> unify_names => :C)
##============================

## But I have to write this
function unify_names(A::String, B::String)
       return joinpath(A,B)
end
transform(df, [:A,:B] => ByRow((x,y)->unify_names(x,y)) => :C)

```

With the second syntax, I have to define the function for each column that I want to add/remove.  
Imagine I want to load the columns conditionally, as in:

```julia
function unify_names(x::DataFrameRow)
       @unpack A = x
       if A=="red"
              return joinpath(A,"banana")
       else 
              @unpack B = x
              return joinpath(A,"banana")
       end

end

```

The function defined on DataFrameRow is soooo much flexible! Is it possible to use it? How?

Thanks!

---

<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: [April 30, 2023, 11:21am UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118/2 "2023-04-30T11:21:20Z")

</div>

You want `AsTable`

```julia
transform(df, AsTable([:A, :B]) => ByRow(unify_names) => :C)

```

With DataFramesMeta.jl it’s

```julia
@rtransform :C = unify_names(AsTable([:A, :B]))

```

Also, do you really want `joinpath`? Why not just do `string(a, b)`?

---

<div class="post-metadata">

### Author: ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)
#### Post date: [April 30, 2023, 7:31pm UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118/3 "2023-04-30T19:31:46Z")

</div>

Thanks!

Yes, `AsTable(propertynames(df))` was exactly what I was looking for!

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [April 30, 2023, 9:40pm UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118/4 "2023-04-30T21:40:44Z")

</div>

alternative ways

```julia

transform(df, [:A,:B]=>ByRow((x,y)->join(x,y)))

transform(df, names(df)=>(x...)->string.(x...))

transform(df, Cols(:)=>(x...)->string.(x...))

transform(df, Cols(:)=>(x...)->.*(x...))

df.C= (*).(eachcol(df)...)

```

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [May 1, 2023, 4:18pm UTC](https://discourse.julialang.org/t/passing-dataframerow-to-transform-in-split-apply-combine/98118/5 "2023-05-01T16:18:59Z")

</div>

[Query.jl](https://github.com/queryverse/Query.jl) way looks like this:

```julia
new_df = df |> @mutate(C=joinpath(_.A, _.B)) |> DataFrame

```
