I need to do something quite specific and i'm trying to do it the good way , especially i want it to be optimized .
So i have a DataFrame that look like this :
│ Row │ USER_ID │ GENRE_MAIN │ ALBUM_NAME │ ALBUM_ARTIST_NAME │ TOTAL_LISTENED │ TOTAL_COUNT_LISTENED │
│ │ String │ String │ String │ String │ DecFP.Dec128 │ DecFP.Dec128 │
├──────────┼─────────┼───────────────────┼─────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┼────────────────┼──────────────────────┤
│ 1 │ 9s2dsdd6 │ ROCK │ The Thought's Boy │ AARON MADISON │ 5912.0 │ 91.0 │
│ 1 │ 9s2dsdd6 │ ROCK │ The wqeqwewe │ AARON MADISON │ 3212.0 │ 91.0 │
│ 2 │ 11sdasd63 │ ROCK │ Down On The Upside │ SOUNDGARDEN │ 3354.0 │ 14.0 │
│ 3 │ 112sds334 │ CLASSICAL │ Beethoven: Symphonies Nos. 1 & 2 - C.P.E. Bach: Symphonies, Wq 175 & 183/17 │ AKADEMIE FÜR ALTE MUSIK BERLIN │ 1372.0 │ 4.0 │
│ 4 │ 145sdsd42 │ POP │ My Life in the Bush of Ghosts │ BRIAN ENO │ 3531.0 │ 17.0 │
I want to aggregate it by user (i have many rows per user_id ) and do many calculations I'm doing aggregation with this :
gdf = DataFrames.groupby(df, :USER_ID)
combine(gdf,:TOTAL_LISTENED => sum => :TOTAL_SECONDS_LISTENED,
:TOTAL_COUNT_LISTENED => sum => :TOTAL_TRACKS_LISTENED)
I need to calculate the top 1 ,2 ,3 ,4 ,5 genre, album name, artist name per user_id and it has to be like this :
USER_ID │ ALBUM1_NAME │ ALBUM2_NAME ......│ GENRE1 │ GENRE2
One line per user_id .
So i tried to do it with a countmap and then sort it , keep only the top 5 , and assign each value to a column in a Dataframe
transposed = sort(countmap(targetId[targetCol]), byvalue=true, rev=true)
for (i, g) in enumerate(eachcol(transposed))
rVal["ALBUM$(i)_NAME"] = g[1]
rVal["ALBUM$(i)_ARTIST"] = g[3]
rVal["ALBUM$(i)_TIME"] = g[2]
rVal["ALBUM$(i)_ID"] = "ID"
rVal["USER_ID"] = id
end
but it doesn't work in a combine , its just very ugly and im sure i can do it a better way .
I hope its understandable , if someone can help me please =)
Thank you
EDIT : A way to reproduce the DataFrame:
v = ["x","y","z"][rand(1:3, 10)]
df = DataFrame(Any[collect(1:10), v, rand(10)], [:USER_ID, :GENRE_MAIN, :TOTAL_LISTENED])