# Insert dummy 0 value rows into dataframe

**URL:** https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647
**Category:** General Usage
**Tags:** dataframes
**Created:** [May 22, 2021, 4:51pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647 "2021-05-22T16:51:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)
#### Post date: [May 22, 2021, 4:51pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/1 "2021-05-22T16:51:24Z")

</div>

Hello All,

I’m trying to figure out a quick way to insert dummy rows with 0 values if they are missing for a set of index fields

For example, let’s say I have this dataframe:

```julia

data = DataFrame(:index => String[], :metric => String[], :values => Int64[])

push!(data, ["a", "x", 1])
push!(data, ["a", "y", 2])
push!(data, ["b", "x", 1])

julia> data
3×3 DataFrame
 Row │ index metric values 
     │ String String Int64  
─────┼────────────────────────
   1 │ a x 1
   2 │ a y 2
   3 │ b x 1

```

For this table, I have metrics x,y for index A, but only metric X for index B. Is there a simple way to populate this table with index b, metric y, value = 0?

Of course, in my real table, there are thousands/millions of rows, so I don’t want to loop through these values one at a time to insert them. In my current program, I pivot the data wide, insert 0 values where they are missing and then depivot it back, but not sure if there is an easy way to do that/better way to do that in Julia DataFrames.

Thanks.

---

<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: [May 22, 2021, 4:55pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/2 "2021-05-22T16:55:24Z")

</div>

There is a keyword argument `cols` in `push!` where you can do `cols = :union` do add new columns.

However you can’t control what the value will be, so you will need to do `coalesce` after your loop.

---

<div class="post-metadata">

### Author: ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)
#### Post date: [May 22, 2021, 6:33pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/3 "2021-05-22T18:33:18Z")

</div>

Sorry I’m not quite sure what you mean by this. How can I use union in this case?

---

<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: [May 22, 2021, 6:41pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/4 "2021-05-22T18:41:30Z")

</div>

What you want is [https://github.com/JuliaData/DataFrames.jl/pull/1864](https://github.com/JuliaData/DataFrames.jl/pull/1864) but it was never finished. If you would find this functionality useful I can re-write this PR anew and add it.

---

<div class="post-metadata">

### Author: ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)
#### Post date: [May 22, 2021, 7:11pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/5 "2021-05-22T19:11:06Z")

</div>

It does sound like what I want. I’d use the function if it existed.

Thanks

---

<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: [May 22, 2021, 7:15pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/6 "2021-05-22T19:15:04Z")

</div>

For now you can have a look at the internals of the implementation - they should be relatively fast (but I do not guarantee it as it was written a long time ago).

---

<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: [May 22, 2021, 9:22pm UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/7 "2021-05-22T21:22:10Z")

</div>

Please read the docstring for `push!` with `?push`. Here is an MWE that points you in the right direction.

```julia
julia> data = DataFrame(:index => String[], :metric => String[], :values => Int64[])
0×3 DataFrame

julia> datas = [(index = "a", metric = "x"), (index = "a", values = 1)];

julia> for d in datas
           push!(data, d; cols = :union)
       end

julia> data
2×3 DataFrame
 Row │ index metric values  
     │ String String? Int64?  
─────┼──────────────────────────
   1 │ a x missing 
   2 │ a missing 1

```

---

<div class="post-metadata">

### Author: ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)
#### Post date: [May 23, 2021, 1:41am UTC](https://discourse.julialang.org/t/insert-dummy-0-value-rows-into-dataframe/61647/8 "2021-05-23T01:41:37Z")

</div>

My solution, (when columns are categorical `unique` should be changed to `levels`)

```julia
d = DataFrame([(index = i, metric = m) 
                            for i in unique(data.index)
                            for m in unique(data.metric)])

leftjoin(d, data, on =[:index, :metric])

```
