# DataFrames Adding Rows of Different Number of Cols

**URL:** https://discourse.julialang.org/t/dataframes-adding-rows-of-different-number-of-cols/112725
**Category:** Data
**Tags:** question, dataframes
**Created:** [April 9, 2024, 1:15pm UTC](https://discourse.julialang.org/t/dataframes-adding-rows-of-different-number-of-cols/112725 "2024-04-09T13:15:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [April 9, 2024, 1:15pm UTC](https://discourse.julialang.org/t/dataframes-adding-rows-of-different-number-of-cols/112725/1 "2024-04-09T13:15:21Z")

</div>

I’m interested in adding a row to a dataframe where the row may have a different number of columns / different columns than the existing data frame. I get the following error when I push a dictionary:  
`push!(mydataframe, mydict)`  
error:  
`ArgumentError: row insertion with `cols`equal to`:setequal`requires`row`to have the same number of elements as the number of columns in`df`.`  
I’m not sure how to change :setequal ?

mydict has most of the same keys as mydataframe has labeled columns. just some extra.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 9, 2024, 1:18pm UTC](https://discourse.julialang.org/t/dataframes-adding-rows-of-different-number-of-cols/112725/2 "2024-04-09T13:18:11Z")

</div>

Consult the docs:

[Functions · DataFrames.jl](https://dataframes.juliadata.org/stable/lib/functions/#Base.push)!

> If `row` is a `DataFrameRow`, `NamedTuple`, `AbstractDict`, or `Tables.AbstractRow` then values in `row` are matched to columns in `df` based on names. The exact behavior depends on the `cols` argument value in the following way:

- If `cols == :setequal` (this is the default) then `row` must contain exactly the same columns as `df` (but possibly in a different order).
- If `cols == :orderequal` then `row` must contain the same columns in the same order (for `AbstractDict` this option requires that `keys(row)` matches `propertynames(df)` to allow for support of ordered dicts; however, if `row` is a `Dict` an error is thrown as it is an unordered collection).
- If `cols == :intersect` then `row` may contain more columns than `df`, but all column names that are present in `df` must be present in `row` and only they are used to populate a new row in `df`.
- If `cols == :subset` then the behavior is like for `:intersect` but if some column is missing in `row` then a `missing` value is pushed to `df`.
- If `cols == :union` then columns missing in `df` that are present in `row` are added to `df` (using `missing` for existing rows) and a `missing` value is pushed to columns missing in `row` that are present in `df`.

---

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [April 9, 2024, 1:28pm UTC](https://discourse.julialang.org/t/dataframes-adding-rows-of-different-number-of-cols/112725/3 "2024-04-09T13:28:36Z")

</div>

beautiful thanks I can write:  
`push!(mydataframe, mydict, cols=:union)`  
for anyone reading this thread later.
