# How to call a column in DataFrame that has a space in the name

**URL:** <https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [January 8, 2023, 11:29pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687 "2023-01-08T23:29:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [January 8, 2023, 11:29pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/1 "2023-01-08T23:29:58Z")

</div>

Silly question here but just wondering how do you call a column in a `DataFrame` that has a space in its name?  
e.g

```julia
dfs = DataFrame(Symbol("first") => [6,7,8,9], Symbol("first last") => [1,2,3,4])
4×2 DataFrame
 Row │ first first last 
     │ Int64 Int64      
─────┼───────────────────
   1 │ 6 1
   2 │ 7 2
   3 │ 8 3
   4 │ 9 4

```

I can call `first` via

```julia
dfs.first
4-element Vector{Int64}:
 6
 7
 8
 9

```

but how would I call the `first last` column given the whitespace in between?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 8, 2023, 11:35pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/2 "2023-01-08T23:35:58Z")

</div>

First idea:

```julia
julia> getproperty(dfs, :"first last")
4-element Vector{Int64}:
 1
 2
 3
 4

```

Second:

```julia
julia> dfs[!,"first last"]

```

Lastly, perhaps change the design not to have to do so?

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [January 9, 2023, 1:10am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/3 "2023-01-09T01:10:30Z")

</div>

thanks! yes I realized a little late that it was a poor design choice.

---

<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:** [January 9, 2023, 6:21am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/4 "2023-01-09T06:21:06Z")

</div>

This is normal for columns to have spaces in their names. DataFrames.jl was designed to handle them fully - you just need to use strings instead of symbols:

```julia
julia> dfs = DataFrame("first" => [6,7,8,9], "first last" => [1,2,3,4])
4×2 DataFrame
 Row │ first first last
     │ Int64 Int64
─────┼───────────────────
   1 │ 6 1
   2 │ 7 2
   3 │ 8 3
   4 │ 9 4

julia> dfs."first last"
4-element Vector{Int64}:
 1
 2
 3
 4

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [January 9, 2023, 7:20am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/5 "2023-01-09T07:20:56Z")

</div>

Thanks for pointing that out! I’m linking your post about Strings vs Symbols in DataFrames here in case anyone else needs help understanding the distinction.

> **[Strings vs symbols in DataFrames.jl column indexing](https://bkamins.github.io/julialang/2022/08/05/symbol.html)**
>
> Introduction

---

<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:** [January 9, 2023, 8:03am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/6 "2023-01-09T08:03:17Z")

</div>

Yes - in general in my blog I try to keep answers to all typical questions users might have 😄.

If there is something important that I have not covered yet, please let me know and I will write a post about it.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [January 9, 2023, 11:48pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/7 "2023-01-09T23:48:07Z")

</div>

Awesome thank you so much! I really appreciate it and will let you know if I come across anything not covered.

Sorry I’m a little confused about the different rules under different versions of julia. I assume this is why you mention that “any project should be accompanied by a complete specification of environment.”

So under Julia 1.7.2, DataFrames.jl 1.3.4 “the convenience syntax for `getproperty` using`.` accessor does not work for symbols containing spaces and we need to do an explicit `getproperty` call.”

Is it the case that under Julia v1.8 and DataFrames v1.4.4 the `.` accessor now works on both `Symbol` and `string` for symbols containing spaces without the `get property` call?

```julia
dfsymbl = DataFrame(Symbol("first") => [6,7,8,9], Symbol("first last") => [1,2,3,4])
4×2 DataFrame
 Row │ first first last 
     │ Int64 Int64      
─────┼───────────────────
   1 │ 6 1
   2 │ 7 2
   3 │ 8 3
   4 │ 9 4

dfsymbl."first last"
4-element Vector{Int64}:
 1
 2
 3
 4

```

```julia
dfstrng = DataFrame("first" => [6,7,8,9], "first last" => [1,2,3,4])
4×2 DataFrame
 Row │ first first last 
     │ Int64 Int64      
─────┼───────────────────
   1 │ 6 1
   2 │ 7 2
   3 │ 8 3
   4 │ 9 4

dfstrng."first last"
4-element Vector{Int64}:
 1
 2
 3
 4

```

I’m not sure if this is a version issue or if I was accidentally generating strings in both cases?

Also under Julia v1.7.2 and DataFrames. jl v1.3.4.

“The second important aspect is that all functions that manipulate column names in DataFrames.jl work with strings. This is natural, as symbol manipulation is not supported by Julia.”

Is it correct to assume that even with future versions of Julia, some DataFrame column name manipulations may continue to work exclusively for `strings` and not work for `Symbols`?

The example provided seems to work with `Symbols` but I wasn’t sure whether this should be regarded as an exception or seen as a rule going forward?

```julia
julia> df = DataFrame(:col1 => 1, Symbol("col 2") => 2)
1×2 DataFrame
 Row │ col1 col 2 
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 2

select(df, Cols(startswith("c")) .=> identity .=> uppercase)
1×2 DataFrame
 Row │ COL1 COL 2 
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 2

```

---

<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:** [January 10, 2023, 7:02am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/8 "2023-01-10T07:02:02Z")

</div>

Can you please refer to the source of your quotations? In general - accessing columns with strings like `df."some column"` worked under Julia 1.7, works under Julia 1.8 and will continue to work. Similarly it worked under DataFrames.jl 1.3, works under DataFrames.jl 1.4 and is planned to continue to work.

Therefore - I am not fully clear what you refer to exactly. Can you please clarify? (the source is likely referring to some other issue - in particular passing strings via variables, but I need to see exactly what you refer to to assess). Thank you!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 10, 2023, 9:12am UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/9 "2023-01-10T09:12:35Z")

</div>

It seems to be a quote from [Strings vs symbols in DataFrames.jl column indexing | Blog by Bogumił Kamiński](https://bkamins.github.io/julialang/2022/08/05/symbol.html).

@phantom I think you misunderstood what it means here to work with symbols or strings. In your examples you check what works with the following data frames:

```julia
dfsymbl = DataFrame(Symbol("first") => [6,7,8,9], Symbol("first last") => [1,2,3,4])
dfstrng = DataFrame("first" => [6,7,8,9], "first last" => [1,2,3,4])

```

but these are exactly the same! It doesn’t matter if you create the columns with a symbol or a string, they are always stored in the same way.

The text that you quote is only talking about column _access_. When you write `df.first` you are actually using the symbol `:first` to access a column. When you write `df."first"` you are using the string `"first"`. The quoted text is simply saying that the symbol version doesn’t work e.g. when there is a space in the name. You can access the column with a symbol value like `Symbol("first last")` by calling `getproperty`, but you cannot use this symbol literally as in the `df.first` syntax.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [January 10, 2023, 7:53pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/10 "2023-01-10T19:53:44Z")

</div>

Got it, thank you so much! This clears up a lot.

I apologize for not making the reference clear. The quotes come from Strings vs symbols in DataFrames.jl column indexing. Given my inadequate background I completely misunderstood the well written post.

I took the following examples to mean that a DataFrame column label was being stored as either a symbol or a string and must then be indexed in accordance with the type with which it was created.

( This sent me down a rabbit hole as to why the columns in @sijo 's example [here](https://discourse.julialang.org/t/run-multiple-instances-of-transform-on-specific-column-combinations-of-a-groupeddataframe-in-dataframes-mini-language/91736/22) could be accessed with a `string` as the `transform` operation output `NamedTuples` as new columns. Which lead to the above post.)

Now my understanding is that no matter how I create the DataFrame Column, it is stored in one way (a symbol?), and can be indexed freely with both symbols and strings.

Sorry don’t mean to create so much confusion on the discussion board. @bkamins book came out on amazon today so I should have a better understanding of things going forward.

> [@](#):
>
> # Strings vs symbols in DataFrames.jl
> 
> Column names in a `DataFrame` are labels. For this reason both symbols and strings are allowed to be used when referencing them without introducing an ambiguity. Here is an example. We start with strings:
> 
> ```julia
> julia> using DataFrames
> 
> julia> df = DataFrame("col1" => 1, "col 2" => 2)
> 1×2 DataFrame
> Row │ col1 col 2
> │ Int64 Int64
> ─────┼──────────────
> 1 │ 1 2
> 
> julia> df."col1"
> 1-element Vector{Int64}:
> 1
> 
> julia> df."col 2"
> 1-element Vector{Int64}:
> 2
> 
> julia> df[:, "col1"]
> 1-element Vector{Int64}:
> 1
> 
> julia> df[:, "col 2"]
> 1-element Vector{Int64}:
> 2
> 
> ```
> 
> Now we try the same with symbols:
> 
> ```julia
> julia> df = DataFrame(:col1 => 1, Symbol("col 2") => 2)
> 1×2 DataFrame
> Row │ col1 col 2
> │ Int64 Int64
> ─────┼──────────────
> 1 │ 1 2
> 
> julia> df.col1
> 1-element Vector{Int64}:
> 1
> 
> julia> getproperty(df, Symbol("col 2"))
> 1-element Vector{Int64}:
> 2
> 
> julia> df[:, :col1]
> 1-element Vector{Int64}:
> 1
> 
> julia> df[:, Symbol("col 2")]
> 1-element Vector{Int64}:
> 2
> 
> ```
> 
> We now see the first difference, that we have already discussed. If column names are all valid variable names symbols are more convenient, however, if they are not (e.g. contain spaces) then using strings is more convenient. As an extreme case, note that the convenience syntax for `getproperty` using`.` accessor does not work for symbols containing spaces and we need to do an explicit `getproperty` call.

But now I understand that all columns are stored the same way

---

<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:** [January 10, 2023, 8:10pm UTC](https://discourse.julialang.org/t/how-to-call-a-column-in-dataframe-that-has-a-space-in-the-name/92687/11 "2023-01-10T20:10:14Z")

</div>

You are welcome to ask - this is what this forum is for.

Your understanding is correct. Quoting the crucial passage from your post above:

> Column names in a `DataFrame` are labels. For this reason both symbols and strings are allowed to be used when referencing them without introducing an ambiguity.

So from user’s perspective you do not need to think how technically column names are stored. Think of them as “labels”. You can use either `Symbol` or string to reference the column.

* * *

Now, given you asked (but this should not matter to you as a user). Internally column names are stored as `Symbol`s, and if you use a string to look-up a column it is internally converted to `Symbol` (but as a user you do not have to think about it). The reason why we internally use `Symbol` is that symbol lookup is faster. However, we also allow strings as they are easier to manipulate. This is explained in Section 6.6 of “Julia for Data Analysis” book (in general - i.e. without specific reference to `DataFrame` object) and then section 8.3 (specifically in `DataFrame`) context.
