logo

Inserting a New Row into a DataFrame in Julia 📂Julia

Inserting a New Row into a DataFrame in Julia

Code

using DataFrames

Unit1 = DataFrame(
    member = ["다영","루다","수빈","진숙"],
    birth = [99,97,96,99],
    height = [161,157,159,162]
)

Unit2 = DataFrame(
    member = ["소정","주연","지연","현정"],
    birth = [95,98,95,94],
    height = [166,172,163,165]
)
WJSN = vcat(Unit1, Unit2)

push!(WJSN, ["다원",97,167])
push!(WJSN, ["연정",99,165])

Let’s run the example code above and check the result.

Concatenating Rows of Two Data Frames with vcat()

julia> Unit1 = DataFrame(
           member = ["다영","루다","수빈","진숙"],
               birth = [99,97,96,99],
           height = [161,157,159,162]
       )
4×3 DataFrame
 Row │ member  birth  height 
     │ String  Int64  Int64  
─────┼───────────────────────
   1 │ 다영       99     161 
   2 │ 루다       97     157 
   3 │ 수빈       96     159 
   4 │ 진숙       99     162 

julia> Unit2 = DataFrame(
           member = ["소정","주연","지연","현정"],
           birth = [95,98,95,94],
           height = [166,172,163,165]
       )
4×3 DataFrame
 Row │ member  birth  height 
     │ String  Int64  Int64  
─────┼───────────────────────
   1 │ 소정       95     166
   2 │ 주연       98     172
   3 │ 지연       95     163
   4 │ 현정       94     165

julia> WJSN = vcat(Unit1, Unit2)
8×3 DataFrame
 Row │ member  birth  height 
     │ String  Int64  Int64  
─────┼───────────────────────
   1 │ 다영       99     161
   2 │ 루다       97     157
   3 │ 수빈       96     159
   4 │ 진숙       99     162
   5 │ 소정       95     166
   6 │ 주연       98     172
   7 │ 지연       95     163
   8 │ 현정       94     165

Of course, the columns of the two data frames must be the same.

Inserting a Row with push!()

julia> push!(WJSN, ["다원",97,167])
9×3 DataFrame
 Row │ member  birth  height 
     │ String  Int64  Int64  
─────┼───────────────────────
   1 │ 다영       99     161
   2 │ 루다       97     157
   3 │ 수빈       96     159
   4 │ 진숙       99     162
   5 │ 소정       95     166
   6 │ 주연       98     172
   7 │ 지연       95     163
   8 │ 현정       94     165
   9 │ 다원       97     167

julia> push!(WJSN, ["연정",99,165])
10×3 DataFrame
 Row │ member  birth  height 
     │ String  Int64  Int64  
─────┼───────────────────────
   1 │ 다영       99     161
   2 │ 루다       97     157
   3 │ 수빈       96     159
   4 │ 진숙       99     162
   5 │ 소정       95     166
   6 │ 주연       98     172
   7 │ 지연       95     163
   8 │ 현정       94     165
   9 │ 다원       97     167
  10 │ 연정       99     165

When adding data with push!(), you must provide an array that matches the number of columns.

Environment

  • OS: Windows
  • julia: v1.6.2