I have a dataframe with the following structure:
|code|score|slots|
|a |50 |0.25 |
|b |66 |0.33 |
|c |20 |0.20 |
|d |28 |0.20 |
|e |23 |0.33 |
What I need to do is to find all the combinations of 'slots' that add up to no more than a given value, say 1.0, then to select the combination with the highest associated combined 'score', and finally output the 'codes' corresponding to the optimal combination. Thanks to this answer I was able to do the first part, but the association with the original dataframe is lost and I do not know how to retrieve the associated information from the 'score' and 'code' columns:
import itertools
comb = [seq for i in range(len(df['slots']), 0, -1) for seq in itertools.combinations(df['slots'], i)
if sum(seq) <= 1]
I was thinking about using dictionaries, but my dataframe will contain identical keys. How can I get a list of keys and values corresponding to the selected combinations?