# Add data to a DataFrame

**URL:** <https://discourse.julialang.org/t/add-data-to-a-dataframe/96467>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [March 22, 2023, 5:39pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467 "2023-03-22T17:39:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgmendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lgmendes/32/34577_2.png) [@lgmendes](https://discourse.julialang.org/u/lgmendes)\
**Post date:** [March 22, 2023, 5:39pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/1 "2023-03-22T17:39:00Z")

</div>

Dear All,

**Description of the problem:** How can I add data to individual elements of a dataframe generated using a template? For example, based on the _rowTemplate_ (see the example code) I create a Dataframe that I want to fill with values (_randDict_) at specific points in the DataFrame. The problem is when I used a _comprehension + push!_ the data is copied “in block” i.e., for each row I have all the data. The objective is to have one element per row

**Toy Problem**

```julia
using DataFrames
using Pipe

rowTemplate=DataFrame("a"=>Dict("aa"=>[],"ab"=>[]),"b"=>Dict("ba"=>[],"bb"=>[]),"c"=>Dict("ca"=>[],"cb"=>[]))

dataFrame=@pipe fill(rowTemplate,100)|>vcat(_...)

randVec=rand(1:100,100)
dictRand=[Dict("dic"=>randVecTmp) for randVecTmp in randVec]

[push!(dataFrame[ind,"b"]["ba"],dictRand[ind]) for ind=1:100]

# the objective is to have only one element in the vector and not 100 elements
# i.e., dataFrame[1,"b"]["ba"]=dictRand[1];...;dataFrame[100,"b"]["ba"]=dictRand[100];
# and not dataFrame[1,"b"]["ba"]=dictRand; ....; dataFrame[100,"b"]["ba"]=dictRand;
dataFrame[1,"b"]["ba"]==dictRand # is true

```

---

<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:** [March 22, 2023, 7:05pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/2 "2023-03-22T19:05:32Z")

</div>

It is not fully clear what you want. Could you please show your code in code-block and make it runnable + show what you want instead what you get?

Thank you!

---

<div class="post-metadata">

**Author:** ![lgmendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lgmendes/32/34577_2.png) [@lgmendes](https://discourse.julialang.org/u/lgmendes)\
**Post date:** [March 22, 2023, 10:20pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/3 "2023-03-22T22:20:18Z")

</div>

Thanks for the advice! I have already updated the post according to your suggestion

---

<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:** [March 22, 2023, 11:40pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/4 "2023-03-22T23:40:53Z")

</div>

Instead of:

> [@lgmendes](#):
>
> `dataFrame=@pipe fill(rowTemplate,100)|>vcat(_...)`

Try:

```julia
dataFrame=vcat([
  select(rowTemplate, All() .=> deepcopy; renamecols=false)
  for _ in 1:100]...)

```

Which is closer to the intention (IIUC).  
And similarly, instead of:

> [@lgmendes](#):
>
> `[push!(dataFrame[ind,"b"]["ba"],dictRand[ind]) for ind=1:100]`

Try:

```julia
for ind in 1:100
    push!(dataFrame[ind,"b"]["ba"],dictRand[ind])
end

```

Which doesn’t create a vector through comprehension syntax.

I have to admit, the toy problem testing `deepcopy`ing of structures in DataFrame eluded me for a minute. And it is right that a specific `deepcopy` method for DataFrames might be a good addition for DataFrames, similar to the `copy` method. Maybe an issue can be opened.

---

<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:** [March 23, 2023, 7:22am UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/5 "2023-03-23T07:22:30Z")

</div>

OK. So this should solve your problem I think:

```julia
dataFrame = reduce(vcat, deepcopy.(fill(rowTemplate, 100)))

```

---

<div class="post-metadata">

**Author:** ![lgmendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lgmendes/32/34577_2.png) [@lgmendes](https://discourse.julialang.org/u/lgmendes)\
**Post date:** [March 23, 2023, 10:51am UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/6 "2023-03-23T10:51:13Z")

</div>

Thank you!  
Adding a deepcopy solves the problem in the toy and the real case (which is substantially more complex ).

---

<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:** [March 27, 2023, 1:34pm UTC](https://discourse.julialang.org/t/add-data-to-a-dataframe/96467/7 "2023-03-27T13:34:58Z")

</div>

> [@Dan](#):
>
> And it is right that a specific `deepcopy` method for DataFrames might be a good addition for DataFrames, similar to the `copy` method. Maybe an issue can be opened.

could this task not be entrusted to kwarg copycols?  
`copycols= deeptrue`😃
