Hello, I have a Dataframe with a frequency matrix generate about events in relation to a user.
I need to reduce this matrix in unique USER_ID and sum each EVENT CATEGORY - events categories correspond to category 1,2,3,4.
I have tried using often functions in relation with DataFrames, for example, group by, filter or map but I can’t obtain a unique ID with the sum of each category values.
Thanks for the help!
nilshg
September 16, 2020, 6:16am
#2
If I understand you correctly you just want to groupby
the USER_ID
and then sum the columns? You can do this:
julia> using DataFrames
julia> df = hcat(DataFrame(USER_ID = rand(100:110, 20)), DataFrame(rand(0:1, 20, 10)))
20×11 DataFrame
│ Row │ USER_ID │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 107 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │
│ 2 │ 100 │ 0 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │
│ 3 │ 109 │ 0 │ 1 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 0 │
│ 4 │ 102 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │
│ 5 │ 103 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 6 │ 109 │ 1 │ 0 │ 1 │ 0 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 7 │ 101 │ 1 │ 1 │ 0 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 8 │ 101 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 0 │ 0 │ 0 │
│ 9 │ 102 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 10 │ 106 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │
│ 11 │ 100 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │
│ 12 │ 105 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 13 │ 101 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │
│ 14 │ 109 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 15 │ 107 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 1 │ 1 │
│ 16 │ 101 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │
│ 17 │ 102 │ 0 │ 0 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 18 │ 104 │ 0 │ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │
│ 19 │ 105 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 20 │ 104 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │ 1 │
julia> to_group = names(df[!, Not(:USER_ID)])
10-element Array{String,1}:
"x1"
"x2"
"x3"
"x4"
"x5"
"x6"
"x7"
"x8"
"x9"
"x10"
julia> combine(groupby(df, :USER_ID), to_group .=> sum .=> to_group)
9×11 DataFrame
│ Row │ USER_ID │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 107 │ 0 │ 1 │ 1 │ 2 │ 2 │ 2 │ 1 │ 1 │ 2 │ 1 │
│ 2 │ 100 │ 1 │ 2 │ 1 │ 0 │ 1 │ 2 │ 1 │ 1 │ 0 │ 1 │
│ 3 │ 109 │ 1 │ 1 │ 2 │ 2 │ 3 │ 3 │ 1 │ 0 │ 2 │ 0 │
│ 4 │ 102 │ 1 │ 1 │ 3 │ 2 │ 1 │ 2 │ 3 │ 3 │ 3 │ 1 │
│ 5 │ 103 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 6 │ 101 │ 1 │ 3 │ 2 │ 3 │ 4 │ 3 │ 1 │ 1 │ 2 │ 1 │
│ 7 │ 106 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │
│ 8 │ 105 │ 1 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 2 │ 1 │
│ 9 │ 104 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │ 2 │
1 Like
xiaodai
September 16, 2020, 6:17am
#3
An MWE would help Please read: make it easier to help you
Is this what u mean?
using DataFrames
using DataConvenience: @>
@> df begin
groupby(:USER_ID)
combine( 1:12 .=> sum)
end
1 Like
Thanks @nilshg , that is the solution!!
1 Like