AVA
AVA

Reputation: 2568

How to add column values in each row in DataFrames?

DataFrame, as follows:

julia> df4
135×4 DataFrame
│ Row │ County      │ Year  │ Female │ Male   │
│     │ String      │ Int64 │ Int64⍰ │ Int64⍰ │
├─────┼─────────────┼───────┼────────┼────────┤
│ 1   │ Asotin      │ 2008  │ 1      │ 0      │
│ 2   │ Asotin      │ 2009  │ 0      │ 0      │
│ 3   │ Asotin      │ 2010  │ 0      │ 0      │
│ 4   │ Asotin      │ 2011  │ 0      │ 0      │
│ 5   │ Asotin      │ 2012  │ 0      │ 0      │
│ 6   │ Benton      │ 2008  │ 1      │ 0      │
│ 7   │ Benton      │ 2009  │ 0      │ 0      │
│ 8   │ Benton      │ 2010  │ 0      │ 0      │
│ 9   │ Benton      │ 2011  │ 0      │ 0      │
│ 10  │ Benton      │ 2012  │ 0      │ 0      │
│ 11  │ Chelan      │ 2008  │ 1      │ 0      │
│ 12  │ Chelan      │ 2009  │ 1      │ 0      │
│ 13  │ Chelan      │ 2010  │ 0      │ 1      │
│ 14  │ Chelan      │ 2011  │ 0      │ 0      │
│ 15  │ Chelan      │ 2012  │ 0      │ 2      │
│ 16  │ Clallam     │ 2008  │ 0      │ 0      │
│ 17  │ Clallam     │ 2009  │ 0      │ 0      │
│ 18  │ Clallam     │ 2010  │ 0      │ 0      │
│ 19  │ Clallam     │ 2011  │ 1      │ 1      │
│ 20  │ Clallam     │ 2012  │ 0      │ 0      │
│ 21  │ Clark       │ 2008  │ 0      │ 1      │
⋮
│ 114 │ Thurston    │ 2011  │ 0      │ 0      │
│ 115 │ Thurston    │ 2012  │ 0      │ 0      │
│ 116 │ Walla Walla │ 2008  │ 0      │ 0      │
│ 117 │ Walla Walla │ 2009  │ 0      │ 1      │
│ 118 │ Walla Walla │ 2010  │ 0      │ 0      │
│ 119 │ Walla Walla │ 2011  │ 0      │ 0      │
│ 120 │ Walla Walla │ 2012  │ 0      │ 0      │
│ 121 │ Whatcom     │ 2008  │ 0      │ 0      │
│ 122 │ Whatcom     │ 2009  │ 1      │ 0      │
│ 123 │ Whatcom     │ 2010  │ 0      │ 1      │
│ 124 │ Whatcom     │ 2011  │ 1      │ 1      │
│ 125 │ Whatcom     │ 2012  │ 0      │ 1      │
│ 126 │ Whitman     │ 2008  │ 0      │ 0      │
│ 127 │ Whitman     │ 2009  │ 0      │ 0      │
│ 128 │ Whitman     │ 2010  │ 0      │ 1      │
│ 129 │ Whitman     │ 2011  │ 0      │ 0      │
│ 130 │ Whitman     │ 2012  │ 0      │ 0      │
│ 131 │ Yakima      │ 2008  │ 0      │ 0      │
│ 132 │ Yakima      │ 2009  │ 0      │ 1      │
│ 133 │ Yakima      │ 2010  │ 1      │ 2      │
│ 134 │ Yakima      │ 2011  │ 0      │ 3      │
│ 135 │ Yakima      │ 2012  │ 0      │ 1      │

I would like to sum Male column values and Female column values in each row, as follows:

julia> let
           data = @pipe df4 |>
               select(_, :County, :Year, [:Female, :Male] => ByRow(+) => :Total)
       end

But, I am getting an error, as follows:

ERROR: UndefVarError: ByRow not defined
Stacktrace:
 [1] top-level scope at REPL[53]:2

Please guide me in adding two column values in each row in DataFrames (version 0.19).

Upvotes: 1

Views: 193

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

Let me say for the third (1st, 2nd) time that you should really, really work with the current stable release version of DataFrames - you are trying to use syntax that was added for the 1.0 release, where it works just fine:

julia> using DataFrames

julia> df = DataFrame(a = rand(1:5, 5), b = rand(1:5, 5))
5×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     5      5
   2 │     3      3
   3 │     1      3
   4 │     1      5
   5 │     1      2

julia> select(df, [:a, :b] => ByRow(+) => :Total)
5×1 DataFrame
 Row │ Total 
     │ Int64 
─────┼───────
   1 │    10
   2 │     6
   3 │     4
   4 │     6
   5 │     3

That said, you can just do data[!, :Total] = data.Female .+ data.Male irrespective of DataFrames version.

Upvotes: 3

Related Questions