I’m struggling with transformations in DataFrames.
To make it simple, let’s start with a very basic transformation. Suppose that I want to add columns :b and :c in the following DataFrame:
df = DataFrame(a=repeat([1,2], outer=3), b=repeat([1,2,3], outer=2), c=1:6)
6×3 DataFrame
Row | a | b | c |
---|---|---|---|
Int64 | Int64 | Int64 | |
1 | 1 | 1 | 1 |
2 | 2 | 2 | 2 |
3 | 1 | 3 | 3 |
4 | 2 | 1 | 4 |
5 | 1 | 2 | 5 |
6 | 2 | 3 | 6 |
gdf=groupby(df, :a)
transform(gdf, [:b, :c] => ((b, c) -> b + c) => :b_plus_c)
6×4 DataFrame
Row | a | b | c | b_plus_c |
---|---|---|---|---|
Int64 | Int64 | Int64 | Int64 | |
1 | 1 | 1 | 1 | 2 |
2 | 2 | 2 | 2 | 4 |
3 | 1 | 3 | 3 | 6 |
4 | 2 | 1 | 4 | 5 |
5 | 1 | 2 | 5 | 7 |
6 | 2 | 3 | 6 | 9 |
Now, suppose that, instead of adding two columns, I want to multiply them:
transform(gdf, [:b, :c] => ((b, c) -> b * c) => :b_times_c)
Now I get:
MethodError: no method matching *(::SubArray{Int64, 1, Vector{Int64}, Tuple{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}, false}, ::SubArray{Int64, 1, Vector{Int64}, Tuple{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}, false})
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any…)
@ Base operators.jl:578
*(::AbstractVector, ::LinearAlgebra.AbstractRotation)
@ LinearAlgebra C:\Users\USUARIO\AppData\Local\Programs\Julia-1.9.2\share\julia\stdlib\v1.9\LinearAlgebra\src\givens.jl:19
*(::LinearAlgebra.Diagonal, ::AbstractVector)
@ LinearAlgebra C:\Users\USUARIO\AppData\Local\Programs\Julia-1.9.2\share\julia\stdlib\v1.9\LinearAlgebra\src\diagonal.jl:242
…
If instead of multiplying, I want to calculate the minimum of :b and :c:
transform(gdf, [:b, :c] => ((b, c) -> minimum(b,c)) => :minimum_b_c)
… I get…
MethodError: objects of type SubArray{Int64, 1, Vector{Int64}, Tuple{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}, false} are not callable
Use square brackets for indexing an Array.
My conclusion is that transform
can only be used to add or subtract two columns, but it is unable to multiply or divide them.
Perhaps I am doing something wrong, but I cannot figure out what it is.