# How to create a DataFrame with specific number of columns and rows and fill it with zeros?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [February 21, 2024, 2:38am UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493 "2024-02-21T02:38:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [February 21, 2024, 2:38am UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/1 "2024-02-21T02:38:03Z")

</div>

Hi!  
I’m trying to create a dataframe where the first column have certain number of rows named in the following way:

```julia
df = DataFrame("Names" => names(df_rowname)[2:end])

```

Now I want to add `n` number of columns to `df` with labels names `names(df_colname)`, and fill all the cells with zeros (or another initialization value).  
What is the best way to do it? I can use the first line I wrote here to create the dataframe with the first column, but I’m not sure how to add the rest of them.

Thank you!

---

<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:** [February 21, 2024, 2:51am UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/2 "2024-02-21T02:51:47Z")

</div>

DataFrames are best used to represent data which has several attributes with heterogenous types. It seems that you are going to create some sort of a Matrix with a common type to the data in the columns.

If this is the case, and the names of the rows and columns are important, consider using NamedArrays.jl instead of a DataFrame.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 21, 2024, 3:17am UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/3 "2024-02-21T03:17:53Z")

</div>

Try MWE:

```julia
using DataFrames

df_rowname = DataFrame([(a=1, b=2, c=3, d=4)])

df = DataFrame("Names" => names(df_rowname)[2:end])
foreach(x -> df[!, x] = zeros(nrow(df)), names(df_rowname))
df

```

---

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [February 21, 2024, 4:53am UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/4 "2024-02-21T04:53:03Z")

</div>

Thanks both of you for the helpful insights!

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 21, 2024, 2:05pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/5 "2024-02-21T14:05:53Z")

</div>

Another option:

```julia
row_names = ["a","b","c","d"]
col_names = ["x","y","z"]
zeros_matrix = zeros(length(row_names), length(col_names))
base_df = DataFrame(zeros_matrix, col_names)
row_names_df = DataFrame(rows = row_names)
hcat(row_names_df, base_df)

```

---

<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:** [February 21, 2024, 2:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/6 "2024-02-21T14:29:19Z")

</div>

One more:

```julia
julia> DataFrame(:Names => [:a, :b, :c], ([:x, :y, :z] .=> 0.0)...)
3×4 DataFrame
 Row │ Names x y z       
     │ Symbol Float64 Float64 Float64 
─────┼───────────────────────────────────
   1 │ a 0.0 0.0 0.0
   2 │ b 0.0 0.0 0.0
   3 │ c 0.0 0.0 0.0

```

Or equivalently

```julia
DataFrame([:Names => [:a, :b, :c]; [:x, :y, :z] .=> 0.0])

```

---

<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:** [February 21, 2024, 5:19pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/7 "2024-02-21T17:19:41Z")

</div>

If you want to add columns to an existing data frame you can also use `insertcols!` or `transform!`.

---

<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:** [February 22, 2024, 1:38pm UTC](https://discourse.julialang.org/t/how-to-create-a-dataframe-with-specific-number-of-columns-and-rows-and-fill-it-with-zeros/110493/8 "2024-02-22T13:38:53Z")

</div>

```julia
insertcols!(describe(df_rowname)[2:end,1:1],(names(df_rowname).=>0)...)

```
