# How to create an empty dataframe from a vector of row names and column names

**URL:** <https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [June 25, 2019, 12:54pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650 "2019-06-25T12:54:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [June 25, 2019, 12:54pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/1 "2019-06-25T12:54:20Z")

</div>

Hi,

I would like to create a large empty dataframe of type Floats from a vector of row names and column names. For example

```julia
rn=["row1", "row2", "row3"]
cn=["col1", "col2", "col3"]

```

Thanks !

---

<div class="post-metadata">

**Author:** ![johann.spies](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johann.spies/32/8805_2.png) [@johann.spies](https://discourse.julialang.org/u/johann.spies)\
**Post date:** [June 25, 2019, 2:06pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/2 "2019-06-25T14:06:57Z")

</div>

> [@Fred](#):
>
> rn=[“row1”, “row2”, “row3”] cn=[“col1”, “col2”, “col3”]

It not quite clear to me how a DataFrame containing many columns where the first column column contains many row names can be describe as “empty”.

Do you want this?

```julia
julia> df = DataFrame()
0×0 DataFrame

julia> df[:rowname] = rn
3-element Array{String,1}:
 "row1"
 "row2"
 "row3"
julia> for c in cn
          df[Symbol(c)] = 0.0
          end

julia> df
3×4 DataFrame
│ Row │ rowname │ col1 │ col2 │ col3 │
│ │ String │ Float64 │ Float64 │ Float64 │
├─────┼─────────┼─────────┼─────────┼─────────┤
│ 1 │ row1 │ 0.0 │ 0.0 │ 0.0 │
│ 2 │ row2 │ 0.0 │ 0.0 │ 0.0 │
│ 3 │ row3 │ 0.0 │ 0.0 │ 0.0 │

```

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [June 25, 2019, 2:09pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/3 "2019-06-25T14:09:04Z")

</div>

Thanks @johann.spies your solution is very good !

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [June 25, 2019, 3:44pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/4 "2019-06-25T15:44:50Z")

</div>

In your last example: why is the call to symbol necessary? I’m sure it is; I just don’t understand why.

---

<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:** [June 25, 2019, 3:49pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/5 "2019-06-25T15:49:58Z")

</div>

DataFrames don’t index columns by strings, but rather by Symbols. There’s no super deep reason why it is that way other than that Symbols are more lightweight than strings and have some nice features.

Using a Symbol, for example, makes it easier for `df.a` to refer to column `:a`.

---

<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:** [June 25, 2019, 4:07pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/6 "2019-06-25T16:07:23Z")

</div>

Most importantly, symbols are faster to look up than strings since they are interned.

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [June 25, 2019, 4:23pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/7 "2019-06-25T16:23:29Z")

</div>

Got it. Thanks guys.

---

<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:** [June 25, 2019, 4:24pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/8 "2019-06-25T16:24:16Z")

</div>

An alternative way to do it is:

```julia
julia> df = DataFrame(fill(Float64, length(cn)), Symbol.(cn), length(rn))
3×3 DataFrame
│ Row │ col1 │ col2 │ col3 │
│ │ Float64 │ Float64 │ Float64 │
├─────┼──────────────┼──────────────┼──────────────┤
│ 1 │ 1.0735e-313 │ 1.07357e-313 │ 1.0735e-313 │
│ 2 │ 6.61729e-316 │ 7.6592e-316 │ 3.53922e-316 │
│ 3 │ 7.6592e-316 │ 7.6592e-316 │ 3.53922e-316 │

julia> df.rowname = rn
3-element Array{String,1}:
 "row1"
 "row2"
 "row3"

julia> df
3×4 DataFrame
│ Row │ col1 │ col2 │ col3 │ rowname │
│ │ Float64 │ Float64 │ Float64 │ String │
├─────┼──────────────┼──────────────┼──────────────┼─────────┤
│ 1 │ 1.0735e-313 │ 1.07357e-313 │ 1.0735e-313 │ row1 │
│ 2 │ 6.61729e-316 │ 7.6592e-316 │ 3.53922e-316 │ row2 │
│ 3 │ 7.6592e-316 │ 7.6592e-316 │ 3.53922e-316 │ row3 │

```

A side note for the other solution is that using `df[:x] = 0.0` syntax will soon be not recommended and it will become `df[:x] .= 0.0` soon (using broadcasting).

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [June 25, 2019, 4:37pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/9 "2019-06-25T16:37:27Z")

</div>

Thank you @bkamins your solution is great too !

I have a question : what is the best way to change some values in the dataframe using the colnames and the rownames ? I tried something like  
`df[[:rowname == "row1"], :col3] = 5`

---

<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:** [June 25, 2019, 4:45pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/10 "2019-06-25T16:45:45Z")

</div>

I assume the row-name is unique. In this case what you should do is:

```julia
df[findfirst(=="row1", df.rowname), :col3] = 5

```

Alternative way to write is e.g.:

```julia
df.col3[df.rowname .== "row1"] .= 5

```

This will also work if the column names are not unique.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [June 25, 2019, 4:47pm UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/11 "2019-06-25T16:47:26Z")

</div>

@bkamins very interesting ! 😉 thanks !

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [June 27, 2019, 6:29am UTC](https://discourse.julialang.org/t/how-to-create-an-empty-dataframe-from-a-vector-of-row-names-and-column-names/25650/12 "2019-06-27T06:29:45Z")

</div>

It is also possible to change one column at once 🤓

```julia
julia> df.col3 .= [1,2,3]
3-element Array{Float64,1}:
 1.0
 2.0
 3.0

julia> df
3×3 DataFrame
│ Row │ col1 │ col2 │ col3 │
│ │ Float64 │ Float64 │ Float64 │
├─────┼──────────────┼──────────────┼─────────┤
│ 1 │ 9.88131e-324 │ 6.92857e-310 │ 1.0 │
│ 2 │ 4.94066e-324 │ 6.92857e-310 │ 2.0 │
│ 3 │ 6.92859e-310 │ 6.92857e-310 │ 3.0 │

```
