Home > Net >  How do I calculate a new column and add to dataframe in Julia?
How do I calculate a new column and add to dataframe in Julia?

Time:11-08

(New to Julia)

I'm trying to run this operation. Here's a minimal working example:

df = DataFrame(A = 1:4)

Row A
Int64
1   1
2   2
3   3
4   4

Just a dataframe with four values, 1-4. I want to add a new column where each value is equal to the element, plus the previous elements. In other words, I want:

Row A Row B
Int64 Int64
1   1   1
2   2   3
3   3   6
4   4   10

How can I do this?

I can write a function that calculates the desired number:

function first(j)
    val = 0
    while j != 0
        val  = df.A[j]
        j -= 1
    end
    return val
end

Here j is the index of the element. This question also gives how to add a column after it's been calculated. However, I can't figure out how to turn these values into a new column. I suspect there should be an easier way than calculating the numbers, forming a column with it and then adding it to the dataframe, as well.

CodePudding user response:

julia> df.B = cumsum(df.A);

julia> df
4×2 DataFrame
 Row │ A      B
     │ Int64  Int64
─────┼──────────────
   1 │     1      1
   2 │     2      3
   3 │     3      6
   4 │     4     10

CodePudding user response:

If you want to use your function here's a way. See list comprehensions and map.

using DataFrames

julia> df.B = [first(i) for i in 1:4]
4-element Vector{Int64}:
  1
  3
  6
 10

julia> df
4×2 DataFrame
 Row │ A      B
     │ Int64  Int64
─────┼──────────────
   1 │     1      1
   2 │     2      3
   3 │     3      6
   4 │     4     10
  • Related