# Cannot insert the row to the data frame

**URL:** <https://discourse.julialang.org/t/cannot-insert-the-row-to-the-data-frame/68909>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [September 29, 2021, 8:07am UTC](https://discourse.julialang.org/t/cannot-insert-the-row-to-the-data-frame/68909 "2021-09-29T08:07:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [September 29, 2021, 8:07am UTC](https://discourse.julialang.org/t/cannot-insert-the-row-to-the-data-frame/68909/1 "2021-09-29T08:07:50Z")

</div>

I cannot insert the new row to the dataframe. Any suggestions? Thanks!

```julia
julia> my_data = DataFrame(A = repeat(["test"], 5), B = repeat([100], 5), C = repeat(["100%"], 5))
5×3 DataFrame
 Row │ A B C      
     │ String Int64 String 
─────┼───────────────────────
   1 │ test 100 100%
   2 │ test 100 100%
   3 │ test 100 100%
   4 │ test 100 100%
   5 │ test 100 100%

julia> new = pushfirst!(Array{Any, 1}(missing, ncol(my_data)-1), "new_test")
3-element Vector{Any}:
 "new_test"
 missing
 missing

julia> insert!.(eachcol(my_data), 3, new)
ERROR: MethodError: Cannot `convert` an object of type Missing to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::Ptr) where T<:Integer at pointer.jl:23
  convert(::Type{IT}, ::GeometryBasics.OffsetInteger) where IT<:Integer at /Users/xzhong/.julia/packages/GeometryBasics/wFus0/src/offsetintegers.jl:40
  convert(::Type{T}, ::SentinelArrays.ChainedVectorIndex) where T<:Union{Signed, Unsigned} at /Users/xzhong/.julia/packages/SentinelArrays/VbnBp/src/chainedvector.jl:209
  ...

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [September 29, 2021, 8:35am UTC](https://discourse.julialang.org/t/cannot-insert-the-row-to-the-data-frame/68909/2 "2021-09-29T08:35:19Z")

</div>

This should work

```julia
push!(my_data,("mi",3,"ma"))

```

I suggest that you do not name a vector `new` as this is a reserved keyword (in some contexts) in Julia.  
Also, the types of your new row are not compatible with the DataFrame.  
Try this

```julia
allowmissing!(my_data)
nw = pushfirst!(Array{Any, 1}(missing, ncol(my_data)-1), "new_test")
push!(my_data,Tuple(nw)) 

```

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [September 29, 2021, 8:47am UTC](https://discourse.julialang.org/t/cannot-insert-the-row-to-the-data-frame/68909/3 "2021-09-29T08:47:48Z")

</div>

Solved! Thanks!  
The key point learned from you is:

```julia
allowmissing!(my_data)

```

Test:

```julia
julia> my_data = DataFrame(A = repeat(["test"], 5), B = repeat([100], 5), C = repeat(["100%"], 5))
5×3 DataFrame
 Row │ A B C      
     │ String Int64 String 
─────┼───────────────────────
   1 │ test 100 100%
   2 │ test 100 100%
   3 │ test 100 100%
   4 │ test 100 100%
   5 │ test 100 100%

julia> nw = pushfirst!(Array{Any, 1}(missing, ncol(my_data)-1), "new_test")
3-element Vector{Any}:
 "new_test"
 missing
 missing

julia> allowmissing!(my_data)
5×3 DataFrame
 Row │ A B C       
     │ String? Int64? String? 
─────┼──────────────────────────
   1 │ test 100 100%
   2 │ test 100 100%
   3 │ test 100 100%
   4 │ test 100 100%
   5 │ test 100 100%

julia> insert!.(eachcol(my_data), 3, nw)
3-element Vector{Vector{T} where T}:
 Union{Missing, String}["test", "test", "new_test", "test", "test", "test"]
 Union{Missing, Int64}[100, 100, missing, 100, 100, 100]
 Union{Missing, String}["100%", "100%", missing, "100%", "100%", "100%"]

julia> my_data
6×3 DataFrame
 Row │ A B C       
     │ String? Int64? String? 
─────┼────────────────────────────
   1 │ test 100 100%
   2 │ test 100 100%
   3 │ new_test missing missing 
   4 │ test 100 100%
   5 │ test 100 100%
   6 │ test 100 100%

```
