lundi 10 février 2020

Python - [Pandas / Lists / Numpy?] Select recipes randomly to create menu with nutrient range

I try to create a simple script for myself where I have a database with my own recipes and generate day menu's for the week. This is a sample database:

recipe_data = {'Recipe': ['Macaroni', 'Mashed_potato', 'Risotto', 'Sandwich', 'Tuna_salad', 'Pancakes', 'Overnight_oats'],
        'Recipe_type': ['Dinner', 'Dinner', 'Dinner',  'Lunch', 'Lunch', 'Breakfast', 'Breakfast'],
        'Protein': [50, 40, 30, 20, 20, 10, 10],
        'Carbohydrates': [100, 80, 60, 50, 40, 30, 20]}

df = pd.DataFrame(recipe_data)

           Recipe Recipe_type  Protein  Carbohydrates
0        Macaroni      Dinner       50            100
1   Mashed_potato      Dinner       40             80
2         Risotto      Dinner       30             60
3        Sandwich       Lunch       20             40
4      Tuna_salad       Lunch       20             40
5        Pancakes   Breakfast       10             20
6  Overnight_oats   Breakfast       10             20

I want to create a a function that I could say: generate a random menu [breakfast, lunch and dinner combi] with a protein combination sum between 65 - 85 and a carbohydrate combination sum between: 135 - 165. In this example it would generate an option like:

           Recipe Recipe_type  Protein  Carbohydrates
0        Macaroni      Dinner       50            100
3        Sandwich       Lunch       20             40
5        Pancakes   Breakfast       10             20

I'm very new to python and I saw some answers that look like my question but couldnt find a way to do it. So any help is very welcome. Untill now I only got to this:

print(df.groupby(['Recipe_type'])['Protein'].apply(np.random.choice).reset_index())
  Recipe_type  Protein
0   Breakfast       10
1      Dinner       30
2       Lunch       20

(so obviously this is not printing all the columns and is not taking any sum of nutrients into account..)

it could very well be that I actually should work with lists or dictionairies, so also those siuggestions can help me a lot :)




Aucun commentaire:

Enregistrer un commentaire