logo

How to Change a Specific Value in a DataFrame in Julia 📂Julia

How to Change a Specific Value in a DataFrame in Julia

Overview

To make replacements, use the replace!() method1. The first argument should be the column of the dataframe you want to change, and the second argument takes a pair A => B. It’s important that it’s the dataframe’s column being specified here.

Code

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  보스즈

The example uses the WJSN dataframe as shown above.

julia> replace!(WJSN.member, "진숙" => "여름"); 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  보스즈

The "Jinsuk" in the :member column was changed to "Yeoreum". Note that replace!() was used here, not replace(), and that it was a specific column of the dataframe that was specified.

julia> replace!(WJSN.unit, "보스즈" => "더블랙"); 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  더블랙

The "Bozuz" in the :unit column was uniformly changed to "The Black".

Complete Code

using DataFrames

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],
    unit = ["쪼꼬미","메보즈","쪼꼬미","보스즈","쪼꼬미","메보즈","보스즈","보스즈","쪼꼬미","보스즈"]
)

WJSN

replace!(WJSN.member, "진숙" => "여름"); WJSN

replace!(WJSN.unit, "보스즈" => "더블랙"); WJSN

See Also

Environment

  • OS: Windows
  • julia: v1.6.3