xiaodai
xiaodai

Reputation: 16004

Julia: How to create a new column in DataFrames.jl by adding two columns using `transform` or `@transform`?

using DataFrames

df = DataFrame(a=1:3, b=1:3)

How do I create a new column c such that c = a+b element wise?

Can't figure it out by reading the transform doc.

I know that

df[!, :c] = df.a .+ df.b

works but I want to use transform in a chain like this

@chain df begin
  @transform(c = :a .+ :b)
  @where(...)
  groupby(...)
end

The above syntax doesn't work with DataFramesMeta.jl

Upvotes: 6

Views: 3037

Answers (2)

Gang Liang
Gang Liang

Reputation: 873

The syntax should be:

@chain df begin
  @transform!(@byrow :c = :a + :b)
  ...
end;

Upvotes: 0

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69819

This is an answer using DataFrames.jl.

To create a new data frame:

julia> transform(df, [:a,:b] => (+) => :c)
3×3 DataFrame
 Row │ a      b      c     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      2
   2 │     2      2      4
   3 │     3      3      6

and for an in-place operation:

julia> transform!(df, [:a,:b] => (+) => :c)
3×3 DataFrame
 Row │ a      b      c     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      2
   2 │     2      2      4
   3 │     3      3      6

or

julia> insertcols!(df, :c => df.a + df.b)
3×3 DataFrame
 Row │ a      b      c     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      2
   2 │     2      2      4
   3 │     3      3      6

The difference between transform! and insertcols! is that insertcols! will error if :c column is present in the data frame, while transform! will overwrite it.

Upvotes: 10

Related Questions