# Requiring a NamedTuple instead of just a tuple to return multiple columns from a function for DataFrames.jl seem unintuitive. Discussion

**URL:** <https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691>\
**Category:** Data\
**Tags:** tuple, dataframes, tables\
**Created:** [September 22, 2024, 7:34am UTC](https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691 "2024-09-22T07:34:54Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [September 22, 2024, 7:34am UTC](https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691/1 "2024-09-22T07:34:54Z")

</div>

I tried to read the `transform` doc for DataFrames.jl and I have to say it’s tough going. But I think it contains lots of info.

The situation I want to discuss is actually documented in the doc but the question, how do I return multiple columns from a `function` in the `cols => function => target_cols` triplet syntax?

Based, to make it work you need `function` to return a `NamedTuple` and returning a `Tuple` doesn’t work.

I feel this is unintuitive. What are the considerations that go into making returning a `Tuple` to cause errors? For example, we can check if the output of the `function` is a tuple and dispatch on that somehow right?

```julia
using DataFrames

b=DataFrame(val=rand(100))

function do2(val::AbstractArray)
    println("ok3")
    (meh=val/2, ok=val/3)
end

# approach 1 requiring the function to return NamedTuple worked
b2 = transform(b, :val => do2 => [:a, :b]);

function do3(val::AbstractArray)
    println("ok4")
    (val / 2, val / 3)
end

# approach fails.
b2 = transform(b, :val => do3 => [:a, :b]);

```

PS. how do I return multiple columns using a framework such as DataFramesMeta.jl or DataFramesMacros.jl or TiderData.jl?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 22, 2024, 11:51am UTC](https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691/2 "2024-09-22T11:51:34Z")

</div>

`NamedTupe` of vectors is considered a table that is column-oriented. This is a Tables.jl rule.

Tuple of “something” is considered a table that is row oriented. This is also a Tables.jl rule.

DataFrames.jl inherits these rules.

You get an error because you have a `Tuple` that is column oriented, which is not supported by Tables.jl.

In short - a function does not have to return a `NamedTuple`. Your problem is that `Tuple` follows another pattern of assumed layout of data. But you could return something else, eg. a `Matrix`. Here is an example (which works):

```julia
julia> function do4(val::AbstractArray)
           [val/2 val/3]
       end
do4 (generic function with 1 method)

julia> b2 = transform(b, :val => do4 => [:a, :b]);

```

---

<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:** [September 22, 2024, 12:08pm UTC](https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691/3 "2024-09-22T12:08:46Z")

</div>

Ok. Just feel unintuitive. Wonder why that is the case? As it feels natural to return just a tuple. I would say this is considered somewhat of a quirk.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [September 22, 2024, 1:06pm UTC](https://discourse.julialang.org/t/requiring-a-namedtuple-instead-of-just-a-tuple-to-return-multiple-columns-from-a-function-for-dataframes-jl-seem-unintuitive-discussion/119691/4 "2024-09-22T13:06:28Z")

</div>

I guess @quinnj decided that it is much more typical to have an iterator of rows than an iterator of columns.
