Reputation: 2558
DataFrame:
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 │
Without column renaming, groupby and combine, as follows:
julia> let
data = @pipe df4 |>
groupby(_, :Year) |>
combine(_, :Female => sum, :Male => sum)
end
5×3 DataFrame
│ Row │ Year │ Female_sum │ Male_sum │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼────────────┼──────────┤
│ 1 │ 2008 │ 7 │ 13 │
│ 2 │ 2009 │ 8 │ 11 │
│ 3 │ 2010 │ 4 │ 21 │
│ 4 │ 2011 │ 7 │ 22 │
│ 5 │ 2012 │ 5 │ 31 │
Renaming while combining throws error, as follows:
julia> let
data = @pipe df4 |>
groupby(_, :Year) |>
combine(_, :Female => sum => :Female, :Male => sum => :Male)
end
ERROR: MethodError: objects of type Pair{typeof(sum),Symbol} are not callable
Stacktrace:
[1] fun at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:671 [inlined]
[2] do_call(::DataFrames.var"#fun#173"{Pair{typeof(sum),Symbol}}, ::GroupedDataFrame{DataFrame}, ::Array{Union{Missing, Int64},1}, ::Int64) at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:465
[3] (::DataFrames.var"#174#178"{GroupedDataFrame{DataFrame}})(::Pair{Symbol,Pair{typeof(sum),Symbol}}) at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:698
[4] map(::DataFrames.var"#174#178"{GroupedDataFrame{DataFrame}}, ::Tuple{Pair{Symbol,Pair{typeof(sum),Symbol}},Pair{Symbol,Pair{typeof(sum),Symbol}}}) at ./tuple.jl:158
[5] _combine(::Tuple{Pair{Symbol,Pair{typeof(sum),Symbol}},Pair{Symbol,Pair{typeof(sum),Symbol}}}, ::GroupedDataFrame{DataFrame}) at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:683
[6] combine(::Tuple{Pair{Symbol,Pair{typeof(sum),Symbol}},Pair{Symbol,Pair{typeof(sum),Symbol}}}, ::GroupedDataFrame{DataFrame}) at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:437
[7] combine(::GroupedDataFrame{DataFrame}, ::Pair{Symbol,Pair{typeof(sum),Symbol}}, ::Pair{Symbol,Pair{typeof(sum),Symbol}}) at /opt/julia/julia-1.4.1/share/julia/stdlib/v1.4/packages/DataFrames/yH0f6/src/groupeddataframe/grouping.jl:444
[8] top-level scope at REPL[48]:2
Please guide me in renaming the column name while combining in DataFrame0.19.
Upvotes: 1
Views: 88
Reputation: 13800
As I said on one of your previous questions here, you really shouldn't be using DataFrames 0.19 at this point, the current release is 1.2 and a lot of things have changed since then.
One of these changes was to change the syntax for combine
and transform
to a uniform pattern of :sourcecol => transformation => :targetcol
. In previous versions, the syntax was targetcol = :sourcecol => transformation)
:
(@v1.6) pkg> activate --temp
Activating new environment at `/tmp/jl_59ViXd/Project.toml`
(jl_59ViXd) pkg> add [email protected]
julia> using DataFrames
julia> df = DataFrame(a = rand('a':'b', 10), b = rand(10))
julia> combine(groupby(df, :a), newcol = :b => sum)
2×2 typename(DataFrame)
│ Row │ a │ newcol │
│ │ Char │ Float64 │
├─────┼──────┼─────────┤
│ 1 │ 'a' │ 1.94586 │
│ 2 │ 'b' │ 3.00895 │
But let me reiterate that it's not a good idea to learn outdated and currently unsupported DataFrames syntax if you want to continue using the package!
Upvotes: 3