I have a table like the following:
group_id candidates_id score
1 (a, b, c,g) (0,1,0,1)
2 (d,e,h) (1,0,0)
I want to create a list column named top_candidate where in each group, the only the top2 candidates with the lower scores (only 0, 1) are selected. If there is a tie, retain them all.
The desired table will be:
group_id candidates_id score top_candidate
1 (a, b, c,g) (0,1,0,1) (a,c)
2 (d,e,h) (1,0,0) (e,h)
What would be some possible ways to achieve the desired result. I thought of melting the list but then I was not sure how to track the group level info (each group has its own candidates_id and the sorting by score is done at the level of group_id.