# Print dataframe column names from within a function

**URL:** <https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575>\
**Category:** New to Julia\
**Created:** [February 13, 2020, 2:32pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575 "2020-02-13T14:32:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [February 13, 2020, 2:32pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/1 "2020-02-13T14:32:34Z")

</div>

I have a simple question about printing dataframe names. I apologize if this has already been asked - I searched but could not find an answer.

I sometimes work with Dataframes of 20+ columns, and I cannot remember the names of all the columns. In addition, I like to subset Dataframes for analysis. Thus, I like to print the column names with a colon and comma, to enable an easy copy and paste to subset a dataframe.

Currently, I use a comprehension for this purpose. But for ease of use, I would like to print from within a function. And when I try to print from within a function, I get an extra array of “Nothing” data that fills up the REPL. Is there a way to suppress the extra “Nothing” data from printing within a function?

Here is a MWE:

```julia
# generate 20-column dataframe
using DataFrames
df = DataFrame(col1 = rand(2), col2 = rand(2), col3 = rand(2), col4 = rand(2), col5 = rand(2), col6 = rand(2), col7 = rand(2), col8 = rand(2), col9 = rand(2), col10 = rand(2), col11 = rand(2), col12 = rand(2), col13 = rand(2), col14 = rand(2), col15 = rand(2), col16 = rand(2), col17 = rand(2), col18 = rand(2), col19 = rand(2), col20 = rand(2));

# Method 1 - print names without semicolon (produces printed names and 20-element Array{Nothing, 1})
[print(string(":", x, ", ")) for x in names(df)]

# Method 2 - print names with semicolon (produces printed names only)
[print(string(":", x, ", ")) for x in names(df)];

# Method 3 - use function (produces printed names and 20-element Array{Nothing, 1})
function dfnames(df::DataFrame)
    [print(string(":", x, ", ")) for x in names(df)];
end

dfnames(df)

```

Thanks for any thoughts.

---

<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 13, 2020, 2:40pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/2 "2020-02-13T14:40:20Z")

</div>

The `print` function returns `nothing`, since all it does is print something and not actually return an object.

You are using an array comprehension (`[x for x in items]`) instead of a for loop. It sounds like you want

```julia
function dfnames(df::DataFrame)
   for name in names(df)
       print(":", name, ", ")
   end
end

```

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [February 13, 2020, 3:11pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/3 "2020-02-13T15:11:56Z")

</div>

You could use `join` as in

```julia
julia> df = DataFrame(a = ones(3), b = string.('a':'c'), c = rand(3)) 
3×3 DataFrame
│ Row │ a │ b │ c │
│ │ Float64 │ String │ Float64 │
├─────┼─────────┼────────┼──────────┤
│ 1 │ 1.0 │ a │ 0.849903 │
│ 2 │ 1.0 │ b │ 0.61542 │
│ 3 │ 1.0 │ c │ 0.663782 │

julia> join(string.(':', names(df)), ", ")
":a, :b, :c"

```

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [February 13, 2020, 4:34pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/4 "2020-02-13T16:34:44Z")

</div>

Thank you @pdeffebach and @dmbates - both excellent answers and solutions that I will use.

Just for my own education, do you know why the following:

```julia
[print(string(":", x, ", ")) for x in names(df)];

```

does not print the array of “Nothing”, yet the following does print the array of nothing?

```julia
function dfnames(df::DataFrame)
    [print(string(":", x, ", ")) for x in names(df)];
end

```

Is it because the semicolon is ignored inside a function?

Thanks again.

---

<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 13, 2020, 8:21pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/5 "2020-02-13T20:21:37Z")

</div>

Your function returns the value of the array, so in the REPL it’s printing the output. If you call `dfnames(df);` you will get the same behavior (not printing an array of `nothing`s).

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [February 14, 2020, 1:13pm UTC](https://discourse.julialang.org/t/print-dataframe-column-names-from-within-a-function/34575/6 "2020-02-14T13:13:34Z")

</div>

Got it, thank you.
