juliajulia-dataframe

Operation with Julia DataFrame


I have a dataframe:

signal   value
weak     100
strong   20
neutral  300

How can I make this calculation:

(weak(100) - strong(20)) / weak+strong+neutral

The result should be: 0.19


Solution

  • I am not sure how exactly you want this to be written but assuming your data frame name is df you can do one of:

    julia> using DataFrames
    
    julia> df = DataFrame(signal=["weak", "strong", "neutral"], value=[100, 20, 300])
    3×2 DataFrame
     Row │ signal   value
         │ String   Int64
    ─────┼────────────────
       1 │ weak       100
       2 │ strong      20
       3 │ neutral    300
    
    julia> weak, strong, neutral = df.value
    3-element Vector{Int64}:
     100
      20
     300
    
    julia> (weak - strong) / (weak + strong + neutral)
    0.19047619047619047
    
    julia> d = Dict(df.signal .=> df.value)
    Dict{String, Int64} with 3 entries:
      "strong"  => 20
      "neutral" => 300
      "weak"    => 100
    
    julia> (d["weak"] - d["strong"]) / (d["weak"] + d["strong"] + d["neutral"])
    0.19047619047619047