logo

Adding a New Column to a DataFrame in Julia 📂Julia

Adding a New Column to a DataFrame in Julia

Code

Let’s say we are given the Cosmic Girls dataframe as follows.

WJSN = DataFrame(
    member = ["다영","다원","루다","소정","수빈","연정","주연","지연","진숙","현정"],
    birth = [99,97,97,95,96,99,98,95,99,94],
    height = [161,167,157,166,159,165,172,163,162,165],
)  

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

The code to add a new column to the dataframe is as follows.

  • dataframe[!, :"column_name"] = values

To add a column for the unit, we get,

WJSN[!, :"unit"] = ["쪼꼬미","메보즈","쪼꼬미","더블랙","쪼꼬미","메보즈","더블랙","더블랙","쪼꼬미","더블랙"]

julia> WJSN
10×4 DataFrame
 Row │ member  birth  height  unit   
     │ String  Int64  Int64   String 
─────┼───────────────────────────────
   1 │ 다영       99     161  쪼꼬미
   2 │ 다원       97     167  메보즈
   3 │ 루다       97     157  쪼꼬미
   4 │ 소정       95     166  더블랙
   5 │ 수빈       96     159  쪼꼬미
   6 │ 연정       99     165  메보즈
   7 │ 주연       98     172  더블랙
   8 │ 지연       95     163  더블랙
   9 │ 진숙       99     162  쪼꼬미
  10 │ 현정       94     165  더블랙

Environment

  • OS: Windows10
  • Version: Julia 1.7.1, DataFrames 1.3.2